[英]javascript: How to check if an element is clickable or not
我天真的方法如下:
function isClickable(id){
elem = document.getElementById(id);
if (elem.nodeName.toLowerCase() == 'a' || typeof(elem.click) != 'undefined'){
return true;
}else{
return false;
}
}
有什么我能做的更好的嗎?
對於大多數元素...
if(e.getAttribute('onclick')!=null){
// clickable
}
對於錨...
if(e.getAttribute('href')!=null){
// clickable
}
然后你有需要更多代碼的表單按鈕,最后你有點擊事件冒泡來處理,所以一個涵蓋所有元素的完美解決方案將是一場噩夢!
但是,如果您只想要簡單的容器和錨點,那么我們可以將上面的邏輯與...
if((e.getAttribute('onclick')!=null)||(e.getAttribute('href')!=null)){
// clickable
}
對於反...
if((e.getAttribute('onclick')===null)&&(e.getAttribute('href')===null)){
// not clickable
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.