Guy*_*Guy 6 javascript clickable
我的幼稚方法如下:
function isClickable(id){
elem = document.getElementById(id);
if (elem.nodeName.toLowerCase() == 'a' || typeof(elem.click) != 'undefined'){
return true;
}else{
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
有什么我可以做的更好的吗?
小智 1
对于大多数元素...
if(e.getAttribute('onclick')!=null){
// clickable
}
Run Code Online (Sandbox Code Playgroud)
对于锚...
if(e.getAttribute('href')!=null){
// clickable
}
Run Code Online (Sandbox Code Playgroud)
然后你有需要更多代码的表单按钮,最后你有点击事件冒泡来处理,所以覆盖所有元素的完美解决方案将是一场噩梦!
但是,如果您只想要简单的容器和锚点,那么我们可以将上面的逻辑与......
if((e.getAttribute('onclick')!=null)||(e.getAttribute('href')!=null)){
// clickable
}
Run Code Online (Sandbox Code Playgroud)
对于相反...
if((e.getAttribute('onclick')===null)&&(e.getAttribute('href')===null)){
// not clickable
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4168 次 |
最近记录: |