如何检查某个类是否只剩下 1 个元素,如果是,则执行操作?
我有一个与 .deleteLink 类的链接,我希望能够知道何时仅保留其中 1 个元素以及何时隐藏该锚点。我认为我不太清楚如何做到这一点,尽管到目前为止,这就是我所得到的:
$('.deleteLink').click(function(){
var $deletes = $('.deleteLink');
$deletes.each(function(index){
alert(index + " : " + $(this).attr('class'));
});
});
编辑
我忘记说这些链接是通过另一个锚点创建的(#addLink )所以我很难找到它们,直到我将代码更改为:
$('.deleteLink').live('click', function(){
仍然当我想隐藏或删除最后一个 .deleteLink 时,任何内容都不会受到影响。
I have a link with the class .deleteLink I'd like to be able to know when only 1 of this elements remain and when that happens hide that anchor. I don't think I have a good idea of how to do this though so far this is what I've got:
$('.deleteLink').click(function(){
var $deletes = $('.deleteLink');
$deletes.each(function(index){
alert(index + " : " + $(this).attr('class'));
});
});
EDIT
I forgot to say that those links are being created via another anchor (#addLink) so I had trouble to get ahold of them until I changed the code to :
$('.deleteLink').live('click', function(){
still when I want to hide or remove the last .deleteLink nothing is affected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您只有一个链接时,以下条件将为
true
:When you will have only one link condition below will be
true
:试试这个:
});
Try this:
});