javascript:如何检查元素是否可点击
我天真的做法如下:
function isClickable(id){
elem = document.getElementById(id);
if (elem.nodeName.toLowerCase() == 'a' || typeof(elem.click) != 'undefined'){
return true;
}else{
return false;
}
}
我还能做更好的事情吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于大多数元素...
对于锚点...
然后您有需要更多代码的表单按钮,最后您需要处理点击事件冒泡,因此覆盖所有元素的完美解决方案将是一场噩梦!
但是,如果您只想要简单的容器和锚点,那么我们可以将上面的逻辑与...结合起来...
反之亦然...
For most elements...
For anchors...
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 combine the logic above with a...
For the reverse...