English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery check element existence (recommended)

JavaScript element existence check: Even if this element is deleted, there is no need to worry about JavaScript code errors;

jQuery element existence check:

The code is as follows:

if(!document.getElementById("preview")) return false;

The object obtained by jQuery $ ("#preview") is always, even if there is no such element on the webpage.

The following code cannot be used:

if($("#preview")){
//do something
}

Therefore, when checking if an element exists on a webpage using jQuery, you should judge according to the length of the obtained element.

The code is as follows:

if($("#preview").length>0){
//do something
}

The above-mentioned is the core code of jQuery checking the existence of elements introduced by the editor for everyone. I hope it will be helpful to everyone. If you have any questions, please leave a message, and the editor will reply to everyone in time.

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)

You May Also Like