我想在第二个div文本上单击li标签
并且还需要单击li span上的第3个div文本
这是我的结构结构ul> li>标签>跨度> div> div> div
$("#PlayListItem").on('click','label',function (){
alert($("#PlayListItem li").find("div:nth-child(2)").text());
});
$("#PlayListItem").on('click','span',function (){
alert($(this).find("div:nth-child(3)").text());
});
提前感谢。
最佳答案
由于存在:nth-child()
作为兄弟姐妹,因此span
伪类选择器将不起作用,因此请使用:nth-of-type()
伪类选择器或jQuery :eq()
伪类选择器来使用索引获取元素。
$("#PlayListItem").on('click','label',function (){
alert($("#PlayListItem li").find("div:eq(1)").text());
});
$("#PlayListItem").on('click','span',function (){
alert($('#PlayListItem li').find("div:eq(2)").text());
});
更新:您可以通过单击元素来引用该元素,从而减少代码。
$("#PlayListItem").on('click','label',function (){
alert($("div:eq(1)", this).text());
});
$("#PlayListItem").on('click','span',function (){
alert($(this).parent().find("div:eq(2)").text());
});