jquery - 使用简写 if 更改属性时遇到问题

标签 jquery shorthand-if

我只是想知道是否可以得到一些指示。

我使用简写 if 来根据 div 是否切换来更改文本。

这就是我的文本更改器:

$(this).text($(this).text() == 'Email Me' ? 'Close Form' : 'Email Me');

我想按照这些思路更改 anchor 的标题属性。我已经尝试过使用以下方法:

$(this).text($(this).attr('title', == 'Email Me' ? 'Close Form' : 'Email Me')); //This throws up syntax errors in firebug because of where the == is placed

我也尝试过如下:

$(this).text($(this).attr('title') == 'Email Me' ? 'Close Form' : 'Email Me'); //This doesn't change the attribute at all.

我想我可能真的很蠢,但我无法弄清楚这一点。我用谷歌搜索了一下,似乎找不到明确的答案。

只是想知道您是否对我可能出错的地方有任何建议。

提前致谢

最佳答案

您不应该使用 attr 来代替吗?

$(this).attr("title", $(this).attr('title') == 'Email Me' ? 'Close Form' : 'Email Me');

而不是:

$(this).text($(this).attr('title') == 'Email Me' ? 'Close Form' : 'Email Me'); //This doesn't change the attribute at all.

关于jquery - 使用简写 if 更改属性时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16801736/

相关文章:

javascript - 一起使用滚动和调整大小处理程序

jquery - 使用 CSS 格式化响应式菜单 (jQuery)

jquery - 无法复制效果

php - 不确定 a 的值是多少

jquery - 使用 jQuery 修复底部的页脚

javascript - Twitter Bootstrap、导航栏、下拉菜单无法正常工作

php - 嵌套简写如果

javascript - if/else 变量存在的简写

javascript - JS 简写 如果执行多个操作