admin管理员组文章数量:1323716
My naive approach is the following:
function isClickable(id){
elem = document.getElementById(id);
if (elem.nodeName.toLowerCase() == 'a' || typeof(elem.click) != 'undefined'){
return true;
}else{
return false;
}
}
Is there anything better I can do?
My naive approach is the following:
function isClickable(id){
elem = document.getElementById(id);
if (elem.nodeName.toLowerCase() == 'a' || typeof(elem.click) != 'undefined'){
return true;
}else{
return false;
}
}
Is there anything better I can do?
Share Improve this question asked Apr 12, 2011 at 7:50 GuyGuy 14.8k27 gold badges72 silver badges88 bronze badges 8- 3 i mean, technically everything is clickable... – Jason Commented Apr 12, 2011 at 7:57
- You're right. Would it help if I rephrase to: "if I click on it, will something happen?" – Guy Commented Apr 12, 2011 at 8:00
- what are you trying to acplish by determining this? – Jason Commented Apr 12, 2011 at 8:01
- Actually Firefox needs a nasty script to make it possible to click some html elements programatically – mplungjan Commented Apr 12, 2011 at 8:01
- My goal is testing - after I do some action, does an element bee clickable (stops being clickable) – Guy Commented Apr 12, 2011 at 8:04
1 Answer
Reset to default 1For most elements...
if(e.getAttribute('onclick')!=null){
// clickable
}
For anchors...
if(e.getAttribute('href')!=null){
// clickable
}
Then you have form buttons which require a little more code, and finally you have click-event bubbling to deal with so a perfect solution covering ALL elements would be a nightmare!
However, if you just want something SIMPLE for containers and anchors only, then we can bine the logic above with a...
if((e.getAttribute('onclick')!=null)||(e.getAttribute('href')!=null)){
// clickable
}
For the reverse...
if((e.getAttribute('onclick')===null)&&(e.getAttribute('href')===null)){
// not clickable
}
本文标签: javascript How to check if an element is clickable or notStack Overflow
版权声明:本文标题:javascript: How to check if an element is clickable or not - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742124952a2421895.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论