jQuery: Checking if next element exists

后端 未结 7 836
庸人自扰
庸人自扰 2020-12-02 18:08

Is there a way to check if an next element exists? Check my code:

if($(\"#people .making-of .mask ul li.current\").next(\"li\") != null) {
    alert(\"Exists         


        
7条回答
  •  南笙
    南笙 (楼主)
    2020-12-02 19:02

    Like the post above says, you need to check the length of the item. Jquery.next() will always return a jquery object, but if there is no next item, it will have a length of 0.

    if($("#people .making-of .mask ul li.current").next("li").length > 0) {
        alert("Exists");
    }
    else {
        alert("Dont exists");
    }
    

提交回复
热议问题