【问题标题】:selecting variable / selector and $(this)选择变量/选择器和 $(this)
【发布时间】:2011-06-17 00:28:46
【问题描述】:
我目前正在尝试优化我拥有的功能,并希望同时选择 $(this) 以及 $(#selector) 或 varName 我在下面列出了我的代码以显示示例,我已经进行了一些搜索并且无法找到任何具体的东西。
$(varName, this).stop().animate({opacity : 1}, 'slow');
感谢所有帮助。
【问题讨论】:
标签:
javascript
jquery
variables
jquery-selectors
【解决方案1】:
如果您将第二个参数传递给$(),那么jQuery 会将其视为要搜索的上下文。您需要使用.add() 方法构建一个jQuery 对象,如下所示:
$("#selector").add(this).add(varName).stop().animate({opacity : 1}, 'slow');
【解决方案2】:
试试这个可能会解决你的问题。
$("#selector").add(this).add(varName).stop(true).animate({opacity : 1}, 'slow');
【解决方案3】:
尝试更改为:
$(varName, this).stop(true).animate({opacity : 1}, 'slow');
【解决方案4】:
假设varName 类似于$('#foo')...
$(this).add(varName).stop().animate({opacity : 1}, 'slow');