javascript怎么判断id是否存在

javascript判断id是否存在的方法:1、创建一个HTML示例文件;2、添加script标签;3、通过“function exist(id){…}”方法判断id是否存在即可。

本文操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。

javascript怎么判断id是否存在?

js 判断id 是否存在的方法

代码如下:

<!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>

【推荐学习:javascript基础教程】

以上就是javascript怎么判断id是否存在的详细内容,更多请关注美工之家9502669.com其它相关文章!