用javascript判断一个html元素是否存在的五种方法:
2015-07-17 09:33
603 查看
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");
}
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");
}
相关文章推荐
- 使用 jsoup 对 HTML 文档进行解析和操作
- json 数据类型
- [JSOI2010][BZOJ1822] Frozen Nova 冷冻波
- js常用插件
- JS判断页面是否出现滚动条的方法
- eclipse关闭javascript校验功能
- js中利用jsonp来跨域
- [LeetCode][JavaScript]Product of Array Except Self
- JavaScript 时间与日期
- JS判断页面是否出现滚动条的方法
- JavaScript调用浏览器打印功能实例分析
- javascript生成不重复的随机数
- JavaScript用select实现日期控件
- JSON与XML优缺点对比分析
- javascript实现树形菜单的方法
- js实现头像图片切割缩放及无刷新上传图片的方法
- javascript封装的sqlite操作类实例
- js+html5实现可在手机上玩的拼图游戏
- JavaScript之AOP编程实例
- JavaScript实现基于十进制的四舍五入实例