开发者

find out that jQuery object really contains an existing node

开发者 https://www.devze.com 2023-02-08 10:28 出处:网络
For example I have following jQuery code: var div1 = $(\"divs\").next(); How do I find out that the div1 object really contains 开发者_如何学Goan existing node? For example, $(\"divs1\") just had n

For example I have following jQuery code:

var div1 = $("divs").next();

How do I find out that the div1 object really contains 开发者_如何学Goan existing node? For example, $("divs1") just had no next siblings.


You can check for the length of the result.

var div1 = $("divs").next();

if ( div1.length )
  {
    //it exists..
  }
else
  {
    // no next found..
    // keep in mind that it might fail due to two reasons
    // a) no next element found
    // b) no divs element found
  }

You are aware that divs is not a valid tag name, right ? (hope it is just an example..)


var div1 = $("divs").next();
if (div1.length != 0) {
  // valid one
}
0

精彩评论

暂无评论...
验证码 换一张
取 消