用JS判断一个html元素是否存在的五种方法
2016-03-15 15:02
483 查看
用javascript判断一个html元素是否存在的五种方法:
1. 判断表单元素是否存在(一)
if("periodPerMonth" in document.theForm){
return true;
}else{
return false;
}
2. 判断页面元素是否存在
if(document.getElementById("XXX")){
//存在
}
3. 判断表单元素是否存在(二)
if(document.theForm.periodPerMonth){
//存在
}
或
if(typeof(document.theForm.periodPerMonth)=="object"){
//存在
}
4. 判断表单是否存在
if(document.theForm){
//存在
}
5.用Jquery写脚本
if ( $("#someID").length > 0 ){
$("#someID").text("hi");
}
1. 判断表单元素是否存在(一)
if("periodPerMonth" in document.theForm){
return true;
}else{
return false;
}
2. 判断页面元素是否存在
if(document.getElementById("XXX")){
//存在
}
3. 判断表单元素是否存在(二)
if(document.theForm.periodPerMonth){
//存在
}
或
if(typeof(document.theForm.periodPerMonth)=="object"){
//存在
}
4. 判断表单是否存在
if(document.theForm){
//存在
}
5.用Jquery写脚本
if ( $("#someID").length > 0 ){
$("#someID").text("hi");
}
相关文章推荐
- json格式数据的请求处理
- JS中 window的用法
- javascript 数组 去重
- javascript笔记2-value与innerHTML区别
- js面向对象
- 编写高性能的JavaScript事件(1)
- JavaScript高级程序设计学习笔记--事件(二)(事件对象--DOM中的事件对象/IE中的事件对象/跨浏览器的事件对象)
- Servlet与Jsp的区别
- 001_JavaScript 错误 - Throw、Try 和 Catch
- JS 获取日期相差天数
- 使用Gson解析复杂的json数据
- JavaScript高级程序设计学习笔记--事件(一)(事件流、事件处理程序/事件侦听器)
- 如何用JS判断推广链接所属的客服
- 7.3 Javascript:表单验证-初识正则
- js 中使用基础地址
- javascript笔记1-links
- 使用cJSON库更新json文件
- JS 交互
- 使用JSONP解决Ajax跨域访问问题
- angular js中option绑定