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其它相关文章!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。