在jQuery中使用过滤器

Tim*_*ees 0 html javascript css jquery

<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>
Run Code Online (Sandbox Code Playgroud)

我写:

$(".click").click(function(){
  alert($(this).parent().filter("[title=here]").attr('href'));
})
Run Code Online (Sandbox Code Playgroud)

但这显示我未定义.如何从当前的DIV获取当前站点?

现场:http://jsfiddle.net/uB4ez/

Guf*_*ffa 7

filter方法从结果中筛选出特定项,您希望使用该find方法在结果中查找元素的子元素:

alert($(this).parent().find("[title=here]").attr('href'));
Run Code Online (Sandbox Code Playgroud)

filter如果您拥有所有childred并且想要保留特定的childred,则将使用该方法.例:

alert($(this).parent().children().filter("[title=here]").attr('href'));
Run Code Online (Sandbox Code Playgroud)