How can I check if append element already exists?
This question already has an answer here:
Just do the next:
Html code
<input id="addImage" type="button" value="Add image"/>
<div id="IndicatorImgDiv">
</div>
Javascript code
$("#addImage").click(function(){
if($("#IndicatorImgDiv img").length == 0){
$('#IndicatorImgDiv').append($('<img />').attr("src", "http://www.thepointless.com/images/reddot.jpg"));
}
});
Here the JSFiddle!
Just change:
$('#IndicatorImgDiv').append($('<img />').attr("src", "/Content/images/reddot.png"));
To:
$('#IndicatorImgDiv').find('img[src$="reddot.png"]').length ||
$('#IndicatorImgDiv').append($('<img />').attr("src", "/Content/images/reddot.png"));
尝试下面的代码。
if($('img').length >= 1){
alert('element exist');
}
链接地址: http://www.djcxy.com/p/14682.html
上一篇: 如何检查div是否存在?
下一篇: 我如何检查append元素是否已经存在?