如何用JS判断网页中某个id的网页元素是否存在

时间:2022-04-13 06:04:16
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta name="generator" content="" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="robots" content="index, follow" />
<meta name="googlebot" content="index, follow" />
<title> </title>
<link rel="stylesheet" type="text/css" href="" media="all" />
<script type="text/javascript">
function exist(id){
var s=document.getElementById(id);
if(s){return true}
else{return false}
}
window.onload=function(){
alert(exist("wrapper"));
alert(exist("111"))
}
</script>
</head>
<body>
<div id="wrapper"></div>
</body>
</html>

  如果在后台生成控件的话,如果有显示与否的控制的话,会出现找不到对象的异常(脚本错误),需要加判断。

如何用JS判断网页中某个id的网页元素是否存在