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是否存在的详细内容。