开发者

find out that jQuery object really contains an existing node

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜