JavaScript下判斷元素是否存在


1. 判斷表單元素是否存在(一) 

if("periodPerMonth" in document.theForm) 
{ 
return true; 
} 
else{ 
return false; 
} 


2. 判斷表單元素是否存在(二) 


if(document.theForm.periodPerMonth) 
{ 
//存在 
} 

或 


if(typeof(document.theForm.periodPerMonth)=="object") 
{ 
//存在 
} 

 

2. 判斷頁面元素是否存在 


if(document.getElementById("XXX")) 
{ 
//存在 
}

 

 

4. 判斷表單是否存在 


if(document.theForm) 
{ 
//存在 
} 

5.用Jquery寫腳本 


if ( $("#someID").length > 0 ) { 
$("#someID").text("hi"); 
} 
轉載的博客地址
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM