开发者

jQuery: how to know the div already exists?

开发者 https://www.devze.com 2023-04-04 04:14 出处:网络
I\'m developing a web application, need to append some text to a specific div(id=div_specific). The div is dynamic generated by program, so I need to verify if the div already exists, otherwise, I hav

I'm developing a web application, need to append some text to a specific div(id=div_specific). The div is dynamic generated by program, so I need to verify if the div already exists, otherwise, I have to create that div first. 开发者_开发技巧How to verify if the div already exists?


if(jQuery("#id_specific").length > 0) {
   ...
}

else {
   ...
}


This should do the trick,

if($('#' + div_specific).length == 1){
  alert("I Exist");
}


Simply:

  if($("#mydivname").length) {
    // do something cool with the div
  } else {
    // create it
    $("#mydivparent").append("<div id='mydivname'>hello world</div>")
    // now do something cool with the new div
  }


if($("#div_specific").length == 1) { /* do stuff */ }
0

精彩评论

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

关注公众号