gpt4 book ai didi

javascript - 为什么javascript是:void(0) considered harmful?

转载 作者:数据小太阳 更新时间:2023-10-29 05:24:13 24 4
gpt4 key购买 nike

谁能告诉我或指出一些资源来解释为什么在超链接中使用 javascript:void(0) 是有害的(尤其是在 Internet Explorer 6 中)?

最佳答案

不推荐在链接中使用 javascript: 关键字。我只设法找到一篇关于为什么它可能有害的文章:

a href=”javascript:void(0);” — avoid the void

但普遍的共识表明您不应该使用它,因为它可能会混淆不支持 javascript 的浏览器,对于其中一些浏览器,它可能被解析为无效链接。

相反,您应该提供一个指向页面的链接,该页面要么围绕 javascript 提供的功能工作,要么显示有关需要 javascript 才能正常工作的站点的消息。在同一个链接上,return false; 从您的事件中,如下所示:

<a href="noscript.html" onclick="doSomething(); return false;">I'm a link</a>

或者,在您的 javascript 代码中使用 return false;preventDefault()returnValue:

element.onclick = function ()
{
/*
// return false is better for most situations (see bobince's comment)
if (event.preventDefault)
event.preventDefault();
else
event.returnValue = false;
*/

doSomething();

return false;
}

关于javascript - 为什么javascript是:void(0) considered harmful?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2264968/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com