我有这样的事情:
if (result.Indicator == 1) {
$('#IndicatorImgDiv').append($('<img />').attr("src", "/Content/images/reddot.png"));
}
现在,这会在我单击按钮时添加一个红点图像,但是当我再次单击按钮时,它将再次添加它。我只希望它在单击按钮时出现一次。如何检查附加元素是否已经存在?
只需执行下一个:
HTML代码
<input id="addImage" type="button" value="Add image"/>
<div id="IndicatorImgDiv">
</div>
JavaScript代码
$("#addImage").click(function(){
if($("#IndicatorImgDiv img").length == 0){
$('#IndicatorImgDiv').append($('<img />').attr("src", "http://www.thepointless.com/images/reddot.jpg"));
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句