js判斷一個元素是否存在的方法


1. 判斷表單元素是否存在(一) 
if("periodPerMonth" in document.theForm){ 
return true; 
}else{ 
return false; 
} 
2. 判斷頁面元素是否存在 (可以用來判斷input等里面的值是否存在)
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"); 
}
--------------------- 
原文:https://blog.csdn.net/heroboxbo/article/details/69248414 

 


免責聲明!

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



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