如何使用jquery判断元素的上一个或下一个元素是否存在?

时间:2022-08-23 23:01:30

suppose I have an <ul> :

假设我有一个

    :

<ul>
    <li></li>
    <li></li>
    <li class="test"></li>
</ul>

How can I judge the .test have its next element with jquery ? like this?:

我如何判断.test是否有jquery的下一个元素?喜欢这个?:

if ($("li.test").next()) { …… }

the strange thing is that ,even I write like above:

奇怪的是,即使我写的如上:

if ($("li.test").next()) {alert("true");}

it still alert "true",but as you see,there is no element next to it ?why this happened?

它仍然警告“真实”,但如你所见,它旁边没有元素?为什么会发生这种情况?

Or what I can do is

或者我能做的是

        for (i = 0; i < $("li").length; i++) {
            if ($("li").eq(i).hasClass("test")) {
                if (i == $("li").length - 1) {
                    alert("true");
                }
            }
        }

presently this could solve my problem,but is there a easy way?

目前这可以解决我的问题,但有一个简单的方法吗?

thank you

4 个解决方案

#1


34  

A jQuery selection will always evaluate to boolean true (as in an if statement). This is because it is not a native Javascript type -- it is an object.

jQuery选择将始终评估为布尔值true(如在if语句中)。这是因为它不是本机Javascript类型 - 它是一个对象。

You need to check the length property of the selection. If it is empty, this will be 0 and will evaluate to false, so the if will not pass. If it is not empty, it will be a positive integer and will therefore evaluate to true, so the conditional will pass.

您需要检查选择的长度属性。如果它为空,则该值为0并将计算为false,因此if将不会通过。如果它不为空,则它将是一个正整数,因此将计算为true,因此条件将通过。

if ($("li.test").next().length) { …… }

#2


6  

if ($("li.test").next().length > 0) { ... }

http://jsfiddle.net/Kfsku/

#3


2  

$("li.test").next().length;

Example

#4


1  

You need to check the length property.

你需要检查长度属性。

if ($("li.test").next().length) {
  //has next 
} else {
  //last elem
}

Keep in mind that jQuery usually returns itself so you can chain. Getting the length property of that object will give you the information

请记住,jQuery通常会返回自己,因此您可以链接。获取该对象的length属性将为您提供信息

#1


34  

A jQuery selection will always evaluate to boolean true (as in an if statement). This is because it is not a native Javascript type -- it is an object.

jQuery选择将始终评估为布尔值true(如在if语句中)。这是因为它不是本机Javascript类型 - 它是一个对象。

You need to check the length property of the selection. If it is empty, this will be 0 and will evaluate to false, so the if will not pass. If it is not empty, it will be a positive integer and will therefore evaluate to true, so the conditional will pass.

您需要检查选择的长度属性。如果它为空,则该值为0并将计算为false,因此if将不会通过。如果它不为空,则它将是一个正整数,因此将计算为true,因此条件将通过。

if ($("li.test").next().length) { …… }

#2


6  

if ($("li.test").next().length > 0) { ... }

http://jsfiddle.net/Kfsku/

#3


2  

$("li.test").next().length;

Example

#4


1  

You need to check the length property.

你需要检查长度属性。

if ($("li.test").next().length) {
  //has next 
} else {
  //last elem
}

Keep in mind that jQuery usually returns itself so you can chain. Getting the length property of that object will give you the information

请记住,jQuery通常会返回自己,因此您可以链接。获取该对象的length属性将为您提供信息