Check if element appended already w/ jQuery
I need to modify a function. When a condition is met an element is appended. I need to add a check to see if this element has been already added, because it can be only added just once.
function checkBox()
{
if ($("#myBox").find('.myClas开发者_开发技巧s').length == 0)
{
$("#myBox").append(
$('<a href="" id="newBox">click</a>')
);
}
}
function checkBox() {
if (!$("#myBox").find('.myClass').length) {
$("#myBox").append('<a href="" id="newBox">click</a>');
}
}
Check working example http://jsfiddle.net/nYbTv/
function checkBox()
{
if ($("#myBox").find('#newBox').length == 0)
{
$("#myBox").append(
$('<a href="" id="newBox">click</a>')
);
}
}
Also note that when passing a serialised HTML string to append()
, you don't have to rewrap it with the jQuery object.
精彩评论