How to determine if id exists in javascript

藏色散人
Release: 2021-11-03 14:29:45
Original
4951 people have browsed it

Javascript method to determine whether the id exists: 1. Create an HTML sample file; 2. Add a script tag; 3. Use the "function exist(id){...}" method to determine whether the id exists. .

How to determine if id exists in javascript

The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.

How does JavaScript determine whether the id exists?

js method to determine whether id exists

The code is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript">
// window.onload=function(){
// alert(exist("getSpanText"));
// alert(exist("111"))
// }
function loadText(){
alert(exist("getSpanText"));
}
function exist(id){
var s=document.getElementById(id);
if(s){
return true
}
else{
return false
}
}
</script>
</head>
<body onload="loadText()">
<span id="getSpanText">Jason</span》
</body>
</html>
Copy after login

[Recommended learning: javascript basic tutorial]

The above is the detailed content of How to determine if id exists in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template