Get the App
SLTechnology News&Howtos  ›  Development  › 

How does javascript determine whether div exists?

Shulou Source: shulou.com Published: 2022-06-02 07:49:22 09月10日 Update

Editor to share with you javascript how to judge the existence of div, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Judgment method: 1, use the "document.getElementById (" id value ")" statement to obtain the div element object according to the specified id value; 2, use the if statement to judge whether div exists, and the syntax "if (div element object) {/ / element exists} else {/ / element does not exist}".

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Js determines whether the div element exists.

When the native js getElementById () and getElementsByTagName () methods manipulate the element, if the manipulated element does not exist, the browser will throw an error and terminate the running of the code, so in order to avoid this situation, you can use the following code to determine whether the element exists when you are unable to determine whether the element to be manipulated exists.

Implementation code

Div element

This is a paragraph.

Var div=document.getElementById ("div"); if (div) {/ / the action code that the element exists console.log ("div element exists");} else {/ / the action code console.log ("div element does not exist") that does not exist;}

The above is all the contents of the article "how to determine the existence of div by javascript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Elements code methods articles content objects statements not much most cases tutorials more paragraphs browsers environments computers knowledge systems industries grammar Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Microsoft macOS Xiaomi Shulou Tech Info