2017-04-05js判断一个元素是否存在的方法
2017-04-05 14:18
741 查看
用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");
}
相关文章推荐
- jquery and js 判断一个元素是否存在
- 用JS判断一个html元素是否存在的五种方法
- 通过JS,Jquery判断某个字符在一个字符串中是否存在的方法
- javascript(jquery)中判断一个元素在页面中是否存在的方法(转载-来自CSDN)
- js 判断一个元素是否在页面中存在
- indexOf() 如何判断一个元素在指定数组中是否存在? 找出指定元素出现的所有位置? indexOf()方法 是正序查找,lastIndexOf()是倒叙查找
- js判断一个对象数组里是否存在某个元素
- js 判断一个元素是否存在
- js判断一个对象数组里是否存在某个元素
- js 判断一个元素是否存在
- js 判断一个元素是否在页面中存在
- 编写js扩展方法判断一个数组中是否包含某个元素
- 如何在前端用js判断一个元素在一个数组中是否存在
- 编写js扩展方法判断一个数组中是否包含某个元素
- 编写js扩展方法实现判断一个数组中是否包含某个元素
- js 判断一个元素是否在页面中存在
- 用JS判断一个html元素是否存在的五种方法
- 编写js扩展方法实现判断一个数组中是否包含某个元素
- 用JS判断一个html元素是否存在的五种方法
- 用JS判断一个html元素是否存在的五种方法