是否有任何方式使用.on()委托事件中的jQuery选择器? 你将如何选择只有直接选择器中的孩子? 以下是一段代码示例:我只想过滤<section>
元素的即时<div>
子元素,并且要求至少其中一个<p>
子元素包含单词«您好»。 在此示例中,只有第二个<div>
将被过滤。问题是,其他<div>
可以后添加,所以事件必须委派。jQuery筛选器()选择与.on委托事件
的使用.live()方法,使其更简单恕我直言,因为我可以用:
$('section > div').filter(function(){return /hello/.test(p) }).live('mouseenter', function(){ ... })
但因为它现在已经过时,其更换。对()只允许纯CSS在委托事件中的类似选择器。 有没有人有任何想法如何根据上述2个条件过滤这些元素(直接的孩子& <p>
包含你好)? 感谢
<section>
<div>
<p>abc</p>
<div>
<p>def</p>
<p>hello</p>
</div>
</div>
<div>
<p>hello world</p>
<p>test</p>
</div>
</section>
编辑:我忘了补充我的JS样品,而且我修改的条件有点使得p:包含(“你好”)不足以作为选择。
$('section').on({
mouseenter: function(){
$(this).css('background-color','red');
}
},
$('div').filter(function(){
var p = $(this).children('p').filter(function(){
return /hello/.test($(this).text());
});
return p.length > 2;
})
);
“'> div ...''” - 这真的有用吗? –
是的,但重读这个问题后,也许你想要的事件上的编辑... – mikakun
感谢您的快速答案,但正如扬说,它不起作用。 HTTP://的jsfiddle。net/WP7aW/>>鼠标结束时,这两个div都是“背景颜色”。 – rgandoin