8

我天真的方法如下:

function isClickable(id){     
     elem = document.getElementById(id);
     if (elem.nodeName.toLowerCase() == 'a' || typeof(elem.click) != 'undefined'){
        return true;     
     }else{
        return false;     
     }
}    

有什么更好的我可以做的吗?

4

1 回答 1

1

对于大多数元素...

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

}
于 2020-03-04T21:48:28.000 回答