我的天真做法如下:
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
}