href=“”、href=“#”和href=“#”有什么区别和 href=“javascript:void(0)”?

发布于 2024-11-28 06:43:09 字数 128 浏览 3 评论 0原文

href=""href="#"href="javascript:void(0)" 有什么区别?
它们有哪些不同的用途?什么时候其中一种比另一种更好?

What is the difference between href="", href="#" and href="javascript:void(0)"?
What are the different uses for them and when is one better than another?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

最近可好 2024-12-05 06:43:09

href="" 将重新加载当前页面

href="#" 将当前页面滚动到顶部

href="javascript: void(0)"< /code> 不会执行任何操作。

您还可以通过使用其他两种方法之一从锚点的单击事件处理程序返回 false 来获得与 javascript: void(0) 相同的效果。

我更喜欢使用 Link 然后将事件处理程序绑定到我的 javascript 中某处的点击侦听器,如下所示

document.getElementById('the-link').onclick = function(){ 
    // Do stuff
    return false;
};

:这样,由于您使用的是 #,即使用户禁用了 javascript,页面也不会重新加载(它只会滚动到顶部),而且我认为它看起来比 JavaScript:无效(0)

href="" will reload the current page

href="#" will scroll the current page to the top

href="javascript: void(0)" will do nothing.

You can get the same effect of javascript: void(0) by returning false from the click event handler of the anchor with either of the other two methods as well.

I prefer to use <a id="the-link" href="#">Link</a> and then bind an event handler to the click listener somewhere in my javascript like:

document.getElementById('the-link').onclick = function(){ 
    // Do stuff
    return false;
};

This way, since you're using #, even if the user has javascript disabled the page won't reload (it will just scroll to the top), and I think it's a lot cleaner looking than javascript: void(0)

旧城空念 2024-12-05 06:43:09

'#' 会将用户带回页面顶部,因此我通常使用void(0)

三个原因。鼓励开发团队中使用 # 不可避免地会导致一些人使用这样调用的函数的返回值:

function doSomething() {
    //some code
    return false;
}

但随后他们忘记在 onclick 中使用 return doSomething() code> 并使用 doSomething()

避免 # 的第二个原因是最终返回 false;如果被调用的函数抛出错误,则不会执行。因此,开发人员还必须记住在被调用函数中适当处理任何错误。

第三个原因是,有些情况下 onclick 事件属性是动态分配的。我更喜欢能够调用函数或动态分配它,而不必专门为一种或另一种附件方法编写函数代码。因此,我的 HTML 标记中的 onclick (或任何内容)如下所示:

onclick="someFunc.call(this)"

OR

onclick="someFunc.apply(this, arguments)"

使用 javascript:void(0) 避免了上述所有令人头疼的问题,而且我还没有发现任何缺点的示例。

因此,如果您是一个单独的开发人员,那么您可以明确地做出自己的选择,但如果您作为一个团队工作,则必须声明:

use href="#",确保onclick< /code> 最后总是包含 return false; ,任何被调用的函数都不会抛出错误,如果将函数动态附加到 onclick 属性,请确保它不会抛出错误返回假。

或者

use href="javascript:void(0)"

第二个显然更容易沟通。

'#' will take the user back to the top of the page, so I usually go withvoid(0).

Three reasons. Encouraging the use of # amoungst a team of developers inevitably leads to some using the return value of the function called like this:

function doSomething() {
    //some code
    return false;
}

But then they forget to use return doSomething() in the onclick and just use doSomething().

A second reason for avoiding # is that the final return false; will not execute if the called function throws an error. Hence the developers have to also remember to handle any error appropriately in the called function.

A third reason is that there are cases where the onclick event property is assigned dynamically. I prefer to be able to call a function or assign it dynamically without having to code the function specifically for one method of attachment or another. Hence my onclick (or on anything) in HTML markup look like this:

onclick="someFunc.call(this)"

OR

onclick="someFunc.apply(this, arguments)"

Using javascript:void(0) avoids all of the above headaches and I haven't found any examples of a downside.

So if you're a lone developer then you can clearly make your own choice but if you work as a team you have to either state:

use href="#", make sure onclick always contains return false; at the end, that any called function does not throw an error and if you attach a function dynamically to the onclick property make sure that as well as not throwing an error it returns false.

OR

use href="javascript:void(0)"

The second is clearly easier to communicated.

无声无音无过去 2024-12-05 06:43:09

href="" 将链接到与您当前所在的页面相同的页面,从而有效地刷新页面。 href="#" 不会刷新页面,但使用 # 会使屏幕移动到页面顶部(浏览器实际上正在寻找没有名称的锚点)。 javascript:void(0) 将阻止链接上发生任何事情。

当我想放入链接但不确定它们去哪里时,例如在创建页面布局时,我使用#。如果我想将表单发布到同一页面,我通常使用表单 action="",但我个人从未使用过 href="" 或 javascript:void(0)。

href="" will link to the same page as the one you are currently on, effectively refreshing the page. href="#" will not refresh the page, but using the # will make the screen move to the top of the page (it is the browser effectively looking for an anchor with no name, ). javascript:void(0) will prevent anything happening on the link at all.

I use # when I want to put links in and I'm not sure where they go, for example when creating a page layout. I usually use a form action="" if I want to post the form to the same page, but I have never personally used href="" or javascript:void(0).

好听的两个字的网名 2024-12-05 06:43:09

如果您使用href="",某些浏览器可能会认为锚标记不是链接,而是锚点。那么它就不会获得链接的行为和事件。即使它在您可以测试的浏览器中工作,也不使用空值更安全。

通常会使用无名书签 href="#"。它与链接到任何书签(例如 href="#contact")相同,只不过无名书签指向页面顶部。如果 Javascript 不起作用,这可以作为一个不错的后备方案,因为它只会带你到顶部,而不是导航到其他地方。有时您会看到开发人员忘记停止链接的默认操作,并且当脚本执行其操作时页面会跳转到顶部。

使用 href="javascript:void(0)" 是一种生成根本不会指向任何地方的 URL 的方法,因此不会跟踪该链接。它工作得很好,但是当您在大多数浏览器中指向链接时,URL 会显示在状态字段中,因此它看起来不太漂亮。

If you use href="", some browsers might think that the anchor tag is not a link, but an anchor. Then it would not get the behaviour and events of a link. Even if it works in the browsers that you can test, it's safer not to use an empty value.

Often the no-name bookmark href="#" is used. It's the same as linking to any bookmark, like href="#contact", except that the no-name bookmark leads to the top of the page. This works as a decent fallback if the Javascript doesn't work, as it only takes you to the top instead of navigating somewhere else. Sometimes you see that the developer forgot to stop the default action of the link, and the page jumps to the top while the script does what it does.

Using href="javascript:void(0)" is a way to produce an URL that doesn't lead anywhere at all, so the link won't be followed. It works well, but as the URL is showed in the status field when you point at the link in most browers, it doesn't look as pretty.

沧笙踏歌 2024-12-05 06:43:09

它们是两个不同的链接:
1. href="" 将用户重定向到根页面。更像是默认页面或索引页面。
2. href="#" 不会重定向用户,也不会执行任何操作,只是更改 URL。它只是为了制作一个类似 URL 的按钮,我的意思是类似锚点的按钮。但无处可去。
3.如果你想用它做一些javascript工作。你可以使用

<button onclick="dothis()">Click me!</button>  
function dothis() {
// do what ever you want to do here
} 

第三种类型,不会改变url,不会重定向!

They are two different links:
1. href="" will redirect the user to the root page. More like the default page or the index page.
2. href="#" Will not redirect the user but also will not do anything but just a change in the URL. Its just to make a URL like button I mean a anchor like button. But no where to go with it.
3. If you want to do some javascript job with it. You can use

<button onclick="dothis()">Click me!</button>  
function dothis() {
// do what ever you want to do here
} 

The third type won't change the url, won't redirect!

残花月 2024-12-05 06:43:09

href 属性定义链接资源的 URL。如果锚标签没有href标签,那么它不会成为超链接。 href 属性具有以下值:

1. Absolute path: move to another site like href="http://www.google.com"
2. Relative path: move to another page within the site like herf ="defaultpage.aspx"
3. Move to an element with a specified id within the page like href="#bottom"
4. href="javascript:void(0)", it does not move anywhere.
5. href="#" , it does not move anywhere but scroll on the top of the current page.
6. href= "" , it will load the current page but some browsers causes forbidden errors.

Note: When we do not need to specified any url inside a anchor tag then use

<a href="javascript:void(0)">Test1</a>

The href attribute defines the URL of the resource of a link. If the anchor tag does not have href tag then it will not become hyperlink. The href attribute have the following values:

1. Absolute path: move to another site like href="http://www.google.com"
2. Relative path: move to another page within the site like herf ="defaultpage.aspx"
3. Move to an element with a specified id within the page like href="#bottom"
4. href="javascript:void(0)", it does not move anywhere.
5. href="#" , it does not move anywhere but scroll on the top of the current page.
6. href= "" , it will load the current page but some browsers causes forbidden errors.

Note: When we do not need to specified any url inside a anchor tag then use

<a href="javascript:void(0)">Test1</a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文