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 */ }
精彩评论