委托jquery-获取父容器
我想选择.section容器中的所有委托jquery-获取父容器,jquery,Jquery,我想选择.section容器中的所有p元素,但只选择包含已单击的a.open链接的元素 $('.section').delegate('a.open', "click", function(){ $(this).parent().filter('p').show(); //I want to select all `p` elements in .section container, but just in th one that containts the a.open link th
p
元素,但只选择包含已单击的a.open链接的元素
$('.section').delegate('a.open', "click", function(){
$(this).parent().filter('p').show(); //I want to select all `p` elements in .section container, but just in th one that containts the a.open link that has been clicked
})
谢谢试试这个:
(向树上移动并在第一个.section
)处停止,(查找由给定选择器筛选的所有子体)
那怎么办
编辑:误解了你对问题的解释
$(this).parents('.section:first').find('p').show();
您可以将delegateTarget作为上下文传递(delegateTarget是传递给handler函数的事件对象的属性,是“处理”委托的DOM元素)
$('.section').delegate('a.open', "click", function(e){
$('p', e.delegateTarget).show()
//This means show all <p> elements in the context defined by e.delegateTarget
})
$('.section').delegate('a.open','click',函数(e){
$('p',e.delegateTarget).show()
//这意味着在e.delegateTarget定义的上下文中显示所有元素
})
查看此小提琴使用
find
而不是filter
filter('p')
选择集合中的所有元素(this)。parent()
,它最多是一个元素。find('p')
选择$(this)的所有后代。parent()
,它们是
元素。请看一下jQuery文档:为什么您认为元素位于表头单元格内?他说“就在包含链接的th中”。我可能会误解,但如果是这样的话,很容易更改选择器文本实际上是“[…]但就在[…]的th中”我认为这只是一个输入错误,应该是“[…],但就在[…]”。公平地说,我只是读了不同的内容。不过我现在确实明白了你的观点。代码改变了这一点,但它没有那么有效(你必须在dom上下移动)。使用jQuery 1.5.2,我不得不使用$('div.my-class',e.liveFired)。show();
$(this).parents('.section:first').find('p').show();
$('.section').delegate('a.open', "click", function(e){
$('p', e.delegateTarget).show()
//This means show all <p> elements in the context defined by e.delegateTarget
})