<div class="main">
<a title="here" href="http://google.com">google.com</a>
<a title="sss" href="www.sss.com">sss</a>
<a title="sss" href="www.sss.com">sss</a>
<a title="sss" href="www.sss.com">sss</a>
<a class="click" href="#">check site</a>
</div>
<div class="main">
<a title="here" href="http://stackoverflow">stackoverflow</a>
<a title="sss" href="www.sss.com">sss</a>
<a title="sss" href="www.sss.com">sss</a>
<a title="sss" href="www.sss.com">sss</a>
<a class="click" href="#">check site</a>
</div>
<div class="main">
<a title="here" href="http://cnn.com">cnn.com</a>
<a title="sss" href="www.sss.com">sss</a>
<a title="sss" href="www.sss.com">sss</a>
<a title="sss" href="www.sss.com">sss</a>
<a class="click" href="#">check site</a>
</div>
我写道:
$(".click").click(function(){
alert($(this).parent().filter("[title=here]").attr('href'));
})
但这显示我未定义。如何从当前 DIV 获取当前站点?
最佳答案
filter
方法从结果中过滤掉特定项,您想使用find
方法在结果中查找元素的子项:
alert($(this).parent().find("[title=here]").attr('href'));
filter
如果您拥有所有子项并想要保留特定的子项,则将使用该方法。示例:
alert($(this).parent().children().filter("[title=here]").attr('href'));
关于javascript - 在 jQuery 中使用过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9841489/