如何禁用所有在 Firefox 和 Chrome 中使用 javascript 链接
我想禁用页面中的所有 href 链接。我正在使用下面的代码,它仅在 IE 中工作正常。
var elems = document.getElementsByTagName("*");
var len = elems.length;
for (i=0; i<len; i++) {
if(elems[i].tagName.toLowerCase() == 'a'){
elems[i].disabled = true;
}
}
但我想在所有浏览器上工作。 有人可以帮我如何克服这个问题吗?
提前致谢。
I want to disable all href links in my page. i am using the below code and it is working fine only in IE.
var elems = document.getElementsByTagName("*");
var len = elems.length;
for (i=0; i<len; i++) {
if(elems[i].tagName.toLowerCase() == 'a'){
elems[i].disabled = true;
}
}
but i want work it on all browsers.
can somebody please help me how to overcome this issue.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不想更改链接的 href,也可以这样做:
此处的工作演示:http: //jsfiddle.net/jfriend00/9YkJQ/。
If you don't want to change the href of the links, one could do this too:
Working demo here: http://jsfiddle.net/jfriend00/9YkJQ/.
这是一个非常古老的线程,但为了防止其他人访问此页面,可以使用以下方法设置禁用属性:
这在所有浏览器中都会被识别,并将禁用链接。但是,在不同的浏览器和浏览器中,对禁用链接触发事件的处理方式有所不同。版本。 FF& Chrome 仍会触发 onclick 事件,而 IE8+(不在兼容模式下)不会触发事件。如果没有连接到锚点的 onclick 事件处理程序,则没有必要这样做,一旦设置了禁用属性,普通的 href 将不会触发。要停止触发单击事件,上面的代码就可以工作。括号不是必需的,就个人而言,我在这种情况下不使用它们,因为我假设括号用于分组或函数。在之前提供的解决方案中似乎没有必要。
This is a very old thread but just in case someone else comes to this page, the disabled attribute can be set by using:
This is recognized in all browsers and will disable the link. However, firing events on a disabled link is handled differently in different browsers & versions. FF & Chrome will still fire the onclick event where IE8+, not in compatibility mode, will not fire events. This is not necessary if there is no onclick event handler wired to the anchor, a normal href will not fire once the disabled attribute has been set. To stop the firing of the click event, the above code would work. The parens are not required and personally, I don't use them in this case as I assume parens are for grouping or for a function. Seems unnecessary in the solution provided before.