login我已经看过href很多次了,但是我不知道那..."> login我已经看过href很多次了,但是我不知道那..."/> login我已经看过href很多次了,但是我不知道那..."> login我已经看过href很多次了,但是我不知道那...">

“ javascript:void(0)”是什么意思?

JavaScript

阿飞小卤蛋

2020-03-09

<a href="javascript:void(0)" id="loginlink">login</a>

我已经看过href很多次了,但是我不知道那是什么意思。

第275篇《“ javascript:void(0)”是什么意思?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

9个回答
前端LEY米亚 2020.03.09

Web开发人员javascript:void(0)之所以使用它,是因为它是防止a标记默认行为的最简单方法void(*anything*)返回undefined,这是一个虚假的值。并且返回假值就像return false标记onclick事件中a阻止其默认行为一样。

因此,我认为这javascript:void(0)是防止a标记默认行为的最简单方法

Itachi伽罗 2020.03.09

void运算符计算给定表达式,然后返回未定义。这样可以避免刷新页面。

蛋蛋樱 2020.03.09

To understand this concept one should first understand the void operator in JavaScript.

The syntax for the void operator is: void «expr» which evaluates expr and returns undefined.

If you implement void as a function, it looks as follows:

function myVoid(expr) {
    return undefined;
}

This void operator has one important usage that is - discarding the result of an expression.

In some situations, it is important to return undefined as opposed to the result of an expression. Then void can be used to discard that result. One such situation involves javascript: URLs, which should be avoided for links, but are useful for bookmarklets. When you visit one of those URLs, many browsers replace the current document with the result of evaluating the URLs “content”, but only if the result isn’t undefined. Hence, if you want to open a new window without changing the currently displayed content, you can do the following:

javascript:void window.open("http://example.com/")
阿飞番长 2020.03.09

用法的使用javascript:void(0)意味着HTML的作者滥用了anchor元素而不是button元素。

通过将href设置为“#”或“ javascript:void(0)”来防止页面刷新,锚点标记通常会与onclick事件一起使用以创建伪按钮。当复制/拖动链接,在新选项卡/窗口中打开链接,添加书签以及JavaScript仍在下载,错误输出或被禁用时,这些值会导致意外行为。这也将不正确的语义传达给辅助技术(例如屏幕阅读器)。在这种情况下,建议改用a <button>通常,您只应使用锚来使用正确的URL进行导航。

资料来源:MDN的<a>Page

GilGreen 2020.03.09

值得一提的是,在检查undefined时有时会看到void 0,这仅仅是因为它需要较少的字符。

例如:

something === undefined

something === void 0

因此,某些缩小方法会将undefined替换为void 0。

LJim 2020.03.09

这是将JavaScript函数添加到HTML链接的一种非常流行的方法。
例如:[Print]您在许多网页上看到链接是这样写的:

<a href="javascript:void(0)" onclick="callPrintFunction()">Print</a>

为什么我们需要href消磨onclick单独可以完成这项工作?因为当用户将鼠标悬停在“打印”文本上时href,光标将变为尖号(ꕯ)而不是指针(👆)。hrefa标签上才会将其验证为超链接。

的替代方法href="javascript:void(0);"是使用href="#"此替代方法不需要在用户浏览器中打开JavaScript,因此更加兼容。

西里A 2020.03.09

标签上应始终带有href 调用返回“ undefined”的JavaScript函数就可以了。因此将链接到“#”。

Internet Explorer 6中没有href的锚标记不会a:hover应用样式。

是的,这是可怕的轻微危害人类罪,但Internet Explorer 6也是这样。

我希望这有帮助。

Internet Explorer 6实际上是危害人类的主要罪行。

卡卡西神奇 2020.03.09

“#”与javascript:void的行为存在巨大差异

“#”将您滚动到页面的顶部,而“ javascript:void(0);” 才不是。

如果您要编码动态页面,这非常重要。用户不希望仅单击页面上的链接就返回顶部。

GOLEY前端 2020.03.09

void运算符计算给定表达式,然后返回undefined

void操作者经常被用来仅获取undefined原始值,通常使用“ void(0)”(这是相当于“ void 0”)。在这些情况下,undefined 可以改用全局变量(假设尚未将其分配给非默认值)。

这里提供了一个解释:voidoperator

The reason you’d want to do this with the href of a link is that normally, a javascript: URL will redirect the browser to a plain text version of the result of evaluating that JavaScript. But if the result is undefined, then the browser stays on the same page. void(0) is just a short and simple script that evaluates to undefined.

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android