什么是href表达式< a href =" javascript:;">< / a>做? [英] What does href expression <a href="javascript:;"></a> do?

查看:202
本文介绍了什么是href表达式< a href =" javascript:;">< / a>做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到网页中不时使用以下href。但是,我不明白这是尝试做什么或技术。有人可以详细说明吗?

I have seen the following href used in webpages from time to time. However, I don't understand what this is trying to do or the technique. Can someone elaborate please?

<a href="javascript:;"></a>


推荐答案

< a> 元素是无效的HTML,除非它具有 href name 属性。

An <a> element is invalid HTML unless it has either an href or name attribute.

如果你想让它正确地呈现为一个链接(即带下划线,手形指针等),那么它只有在 href <时才会这样做/ code>属性。

If you want it to render correctly as a link (ie underlined, hand pointer, etc), then it will only do so if it has a href attribute.

这样的代码有时被用作建立链接的方式,但无需在<$中提供实际的URL c $ c> href 属性。开发人员显然希望链接本身不做任何事情,这是他知道的最简单的方法。

Code like this is therefore sometimes used as a way of making a link, but without having to provide an actual URL in the href attribute. The developer obviously wanted the link itself not to do anything, and this was the easiest way he knew.

他可能在其他地方有一些javascript事件代码,当链接被触发时单击,这将是他想要实际发生的事情,但他希望它看起来像普通的< a> 标记链接。

He probably has some javascript event code elsewhere which is triggered when the link is clicked, and that will be what he wants to actually happen, but he wants it to look like a normal <a> tag link.

一些开发人员出于同样的目的使用 href ='#',但这会导致浏览器跳转到页面顶部,这可能是不被通缉。并且他不能简单地将href留空,因​​为 href =''是一个返回当前页面的链接(即它会导致页面刷新)。

Some developers use href='#' for the same purpose, but this causes the browser to jump to the top of the page, which may not be wanted. And he couldn't simply leave the href blank, because href='' is a link back to the current page (ie it causes a page refresh).

有很多方法可以解决这些问题。在 href 中使用空位的Javascript代码就是其中之一,尽管它不是最佳解决方案,但确实有效。

There are ways around these things. Using an empty bit of Javascript code in the href is one of them, and although it isn't the best solution, it does work.

这篇关于什么是href表达式&lt; a href =&quot; javascript:;&quot;&gt;&lt; / a&gt;做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆