js 判断id 是否存在

<!DOCTYPE html>
<html lang="en">
<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>

posted @ 2016-03-17 11:32  JasonXu_徐晓峰的博客  阅读(4247)  评论(0编辑  收藏  举报