jQuery - 添加类后如何刷新选择器
我在 jsfiddle 中做了这个例子来演示我的问题: http://jsfiddle.net/paulmason411/7E6vG/< /a>
查看如何添加灰色类,但当您第二次悬停时它不应用左边距。我猜测这是因为 $('.block.grey')
选择器是在添加 grey 类之前声明的。
无论如何,我可以让 $('.block.grey')
选择器在添加类后重新解析 dom 吗?
任何帮助都会很棒,干杯!
编辑: 我有一个更复杂的示例
$('.accordion h3').not('.ui-state-active').find('a').live('hover', function(){
,其中动态添加 ui-state-active 类。 bitsMix 指出这段代码正在使 a
生效。所以我已经将其更新为
$('.accordion h3').live('hover', function(){
$(this).not('.ui-state-active').find('a').stop().animate({
并且有效!谢谢你们!
I've made this example in jsfiddle to demonstrate my problem: http://jsfiddle.net/paulmason411/7E6vG/
See how the grey class is added and yet it doesn't apply the left margin when u hover a second time. I'm guessing this is because the $('.block.grey')
selector is declared before the class is grey class is added.
Is there anyway I can get the $('.block.grey')
selector to re-parse the dom after the class is added?
Any help would be great, cheers!
EDIT:
I have a more complicated example
$('.accordion h3').not('.ui-state-active').find('a').live('hover', function(){
Where the ui-state-active
class is added dynamically. bitsMix has pointed out that this code is making the a
live. So I've updated it to
$('.accordion h3').live('hover', function(){
$(this).not('.ui-state-active').find('a').stop().animate({
and it works! Thanks guys!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要将 live 添加到如下所示:
fiddle
you need to add live to the like this:
fiddle
http://jsfiddle.net/bitsmix/7E6vG/2/ 现场演示:)
http://jsfiddle.net/bitsmix/7E6vG/2/ live demo :)
它来了:
jQuery Live
Here it comes:
jQuery Live
您可以使用 .live()
http://jsfiddle.net/7E6vG/1/
You can use .live()
http://jsfiddle.net/7E6vG/1/