---
title: "判断HTML元素"
author: "keml"
date: "2011-12-15T17:46:09Z"
tags: []
categories: ["Web前端"]
slug: "html"
draft: false
lastmod: "2011-12-15T17:51:48Z"
id: 1312518
url: "https://keml.iteye.com/blog/1312518"
---

表单名称: theForm

表单元素: periodPerMonth

 

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 &gt; 0 ){ 

$("#someID").text("hi"); 

} 

-----------------------------END----------------------------------------------------------