2012-07-03 116 views
2

这是我code这个.end()函数有什么问题?

<a href="javascript:void(0);" id="apri-menu"> 
    <span>First</span> 
    <span style="display:none;">Second</span> 
</a>​ 

$('#apri-menu').click(function() { 
    $(this).find('span').first().hide().end().find('span').last().show(); 
    $('#menu-nascosto').show(); 
});​ 

点击我最好的方面来显示第二跨度,但目前看来,.end()做出一些痛苦的链接。

我在哪里错了?

回答

4

.end()所做的是“将jQuery对象返回到其先前的选择状态”。在你的例子中,修改匹配对象的最后一个操作是.first(),因此.end()将时间倒回到之前 - 在.find('span')之后。所以,最终的结果是,如果你写了

$(this).find('span').find('span').last().show(); 

...这显然无法工作,因为没有嵌套<span>在你的标记。

简单地摆脱第二个.find()将解决问题。

1

请尝试以下方法。原因是@Jon说。

$(this).find('span:first').hide().end().find('span:last').show(); 
0

工作演示http://jsfiddle.net/KqdJS/

您可以返回到父.end().parent(),然后表现出来,

您还可以看到上面的反应,希望这有助于:)

代码

$('#apri-menu').click(function() { 
    $(this).find('span').first().hide().end().parent().find('span').last().show(); 

    $('#menu-nascosto').show(); 
});​