简体   繁体   English

jQuery:测试if元素是MATCHED元素的第一个

[英]jQuery: test if element is first of MATCHED elements

I have several divs that are alerts. 我有几个警报的div。 The user dismisses them and after the final one (which is the FIRST in the DOM) I need to trigger some jquery magic. 用户解散它们并在最后一个(这是DOM中的第一个)之后我需要触发一些jquery魔法。

Here's some simplified HTML: 这是一些简化的HTML:

​<body>
<div class="">Bla bla bla</div>
<div class="alert">A</div>
<div class="alert">B</div>
</body>​

Here's what I'm trying to do with jquery... 这是我正在尝试用jquery做的...

$(".alert").click( function() {

if ( $(this).is(".alert:first") ) { 
//do stuff
}

});

It doesn't work. 它不起作用。 The conditional never returns true. 条件永远不会返回true。 The only way I can get it to work is using .index() but that sucks because if the order of the markup changes it will break the script. 我能让它工作的唯一方法是使用.index()但这很糟糕,因为如果标记的顺序发生变化,它将破坏脚本。

What am I doing wrong? 我究竟做错了什么?

EDIT: The solution uses .index() but in a way that I did not realize was possible. 编辑:该解决方案使用.index()但在某种程度上我没有意识到是可能的。 It is relative to the matched selection if used in that way. 如果以这种方式使用, 相对于匹配的选择。 Thanks! 谢谢!

You can code: 你可以编码:

$(".alert").click(function() {
    if ($(".alert").index(this) == 0) {
       // 
    }
});

http://jsfiddle.net/g2gGa/ http://jsfiddle.net/g2gGa/

Try this instead : 试试这个:

if ($(this).hasClass("alert") && $(this).is(":first")) { 
  //do stuff
}

Alternately : 或者:

$(".alert").click( function() {
    if ($(".alert").index(this) == 0) { 
        //do stuff
    }
});

Hope this will help !! 希望这会有所帮助!!

Try this jsfiddle 试试这个jsfiddle

$(".alert").click( function() {

      if (this == $(".alert:first")[0] ) { 
        //do stuff 
           alert(this);
       }

});​

you can try this. 你可以试试这个。

$('.alert').click( function() {

if ( ($(this)[0] === $('.alert').first()[0])) { 
alert('first');
}

});​

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在jQuery中,如何测试某个元素是否是同一类的许多元素中的第一个? - In jQuery, how to test whether an element is the first of many elements of same class? jQuery,使用字符串和变量将元素添加到匹配元素的集合中 - jQuery, Add elements to the set of matched element using string and variable jQuery按名称选择属性仅选择第一个匹配的元素 - jQuery selecting by name attribute only selects first matched element jQuery,在匹配的元素中搜索属性值的元素 - jQuery, search for element with value in attribute among the matched elements 检查jquery中3个元素的行中元素是第一个还是第三个 - Check if element is first or third in rows of 3 elements in jquery 所有匹配元素的 jQuery .html() - jQuery .html() of all matched elements jQuery使用live()或on()突出显示匹配的元素 - jQuery highlight matched elements with live() or on() 使用jQuery,如何获取HTML元素的属性,即不是匹配集的第一个元素? - With jQuery, how to get the attribute for an HTML element i.e. not the first element of a matched set? 在一组匹配元素中的一个元素上触发单击事件,而不是在匹配元素上触发 - Fire click event on one element in a set of matched elements and not on matched elements 如何使用Javascript / Jquery为每个唯一属性值选择第一个匹配的元素? - How to select the first matched element for each unique attribute value with Javascript/Jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM