href=“”、href=“#”和href=“#”有什么区别和 href=“javascript:void(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
href=""
将重新加载当前页面href="#"
将当前页面滚动到顶部href="javascript: void(0)"< /code> 不会执行任何操作。
您还可以通过使用其他两种方法之一从锚点的单击事件处理程序返回 false 来获得与
javascript: void(0)
相同的效果。我更喜欢使用
Link
然后将事件处理程序绑定到我的 javascript 中某处的点击侦听器,如下所示:这样,由于您使用的是
#
,即使用户禁用了 javascript,页面也不会重新加载(它只会滚动到顶部),而且我认为它看起来比JavaScript:无效(0)
href=""
will reload the current pagehref="#"
will scroll the current page to the tophref="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: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 thanjavascript: void(0)
'#' 会将用户带回页面顶部,因此我通常使用
void(0)
。三个原因。鼓励开发团队中使用 # 不可避免地会导致一些人使用这样调用的函数的返回值:
但随后他们忘记在
onclick
中使用return doSomething()
code> 并使用doSomething()
。避免 # 的第二个原因是最终返回 false;如果被调用的函数抛出错误,则不会执行。因此,开发人员还必须记住在被调用函数中适当处理任何错误。
第三个原因是,有些情况下 onclick 事件属性是动态分配的。我更喜欢能够调用函数或动态分配它,而不必专门为一种或另一种附件方法编写函数代码。因此,我的 HTML 标记中的 onclick (或任何内容)如下所示:
OR
使用
javascript:void(0)
避免了上述所有令人头疼的问题,而且我还没有发现任何缺点的示例。因此,如果您是一个单独的开发人员,那么您可以明确地做出自己的选择,但如果您作为一个团队工作,则必须声明:
use href="#"
,确保onclick< /code> 最后总是包含
return false;
,任何被调用的函数都不会抛出错误,如果将函数动态附加到 onclick 属性,请确保它不会抛出错误返回假。或者
第二个显然更容易沟通。
'#' will take the user back to the top of the page, so I usually go with
void(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:
But then they forget to use
return doSomething()
in theonclick
and just usedoSomething()
.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:
OR
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 sureonclick
always containsreturn 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
The second is clearly easier to communicated.
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).
如果您使用
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, likehref="#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.它们是两个不同的链接:
1. href="" 将用户重定向到根页面。更像是默认页面或索引页面。
2. href="#" 不会重定向用户,也不会执行任何操作,只是更改 URL。它只是为了制作一个类似 URL 的按钮,我的意思是类似锚点的按钮。但无处可去。
3.如果你想用它做一些javascript工作。你可以使用
第三种类型,不会改变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
The third type won't change the url, won't redirect!
href 属性定义链接资源的 URL。如果锚标签没有href标签,那么它不会成为超链接。 href 属性具有以下值:
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: