开发者

What's the result of a selector with no matches?

What happens when you use a selector like this:

$('#myelement').remove();

but no element with id 'myelement' is actually part of the page? Is it undefined, 开发者_Python百科should we take care to make sure this does not happen?

Thanks


jQuery will take care of it: no errors will be thrown if element is not on the page. It also the same behavior for other jQ methods - it's not necessary to check if element presents on the page.

If you want make sure that element is on the page:

if($('#myElement').size()) {

}


JQuery will take care of this but i think you want to figure out what you are doing as JQuery may help mask some underlying issue where you believe that item is there?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜