开发者

jQuery: how to know the div already exists?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜