简体   繁体   English

当我希望链接无处链接并使用它来运行脚本时,可以用作“ href”属性吗?

[英]What to use as “href” attribute when i want a link to link nowhere and use it for running a script?

I was used to do href="#" but is there any other way? 我曾经做过href="#"但是还有其他方法吗?

Because clicking on such kind of a link sometimes can turn user to front of a page viewed and I want to load some scripts by clicking on a link. 因为单击此类链接有时可能会使用户转到查看的页面的前面,所以我想通过单击链接来加载一些脚本。

Keep using <a href="#">Click</a> , but use some Javascript/jQuery to prevent the page from jumping: 继续使用<a href="#">Click</a> ,但是使用一些Javascript / jQuery来防止页面跳转:

$('a').click(function() {
    // do whatever
    return false;
}

The return false line will prevent the browser from following the link, which in this case will stop it from jumping to the top of the page. 错误的返回行将阻止浏览器跟踪链接,在这种情况下,这将阻止浏览器跳转到页面顶部。

You should keep the link as an <a> tag, rather than use a span or div , since this is far more semantic (ie users/crawlers will know it's supposed to do something since it's a link). 您应该将链接保留为<a>标记,而不要使用spandiv ,因为这要更加语义化(即,用户/爬网者会知道它应该做某事,因为它是链接)。

You should also avoid using inline Javascript (ie onclick="doSomething()" ) since this is a huge pain if you ever want to change the behaviour, and you also want to make your Javascript as unobtrusive as possible . 您还应该避免使用内联Javascript(即onclick="doSomething()" ),因为如果您想更改行为,这会非常麻烦,并且还希望使Javascript尽可能不引人注目

yes, href="#" makes ou scroll... 是的,href =“#”使ou滚动...

You can try: 你可以试试:

<a href="javascript:void(0);" onclick="doIt(); return false;">Woho</a>

Why not use onclick 为什么不使用onclick

<a href="#" onclick="doMyThing() return false;" />

Similar discussion on SO, href-tag-for-javascript-links-or-javascriptvoid0 关于SO的类似讨论, href-tag-for-javascript-links-or-javascriptvoid0

For a good markup if you want an event on same page. 如果想要在同一页面上进行事件,则为获得good markup So, it's better if you use div or span for this & define your click event on it. 因此,最好使用div or span并在其上定义click event

div{
 color:red;
}

JS JS

$('div').click{function(

)};
href="javascript:void(0)"

谢谢Yaniro

As @Yaniro commented you can use href="javascript:void(0)", the reason for this is: 正如@Yaniro所评论的,您可以使用href =“ javascript:void(0)”,其原因是:

JavaScript Void 0 Explanation JavaScript Void 0说明

Web browsers will try and take whatever is used as a URL and load it. Web浏览器将尝试使用任何用作URL的内容并进行加载。 The only reason we can use a JavaScript Alert statement without loading a new page is because alert is a function that returns a null value. 我们可以在不加载新页面的情况下使用JavaScript Alert语句的唯一原因是alert是一个返回空值的函数。 This means that when the browser attempts to load a new page it sees null and has nothing to load. 这意味着,当浏览器尝试加载新页面时,它会显示为null并且没有任何内容可加载。

The important thing to notice here is that if you ever do use a JavaScript statement as the URL that returns a value, the browser will attempt to load a page. 这里要注意的重要一点是,如果您确实使用JavaScript语句作为返回值的URL,则浏览器将尝试加载页面。 To prevent this unwanted action, you need to use the void function on such statement, which will always return null and never load a new page. 为了防止这种不必要的操作,您需要在此类语句上使用void函数,该函数将始终返回null且永远不会加载新页面。

Extracted from: JavaScript Void 0 Explanation 摘自: JavaScript Void 0说明

You can simply use the onclick attribute in any element, eg 您可以简单地在任何元素中使用onclick属性,例如

<span onclick="foo()">Do something</span>

The habit of using href="#" is a holdover from the early days when browsers did not support onclick generally, only for links (in the technical sense: a elements with href attribute). 使用href="#"的习惯是从早期浏览器就一直保留下来的,这种浏览器通常不支持onclick ,仅用于链接(从技术意义上讲:具有href属性a元素)。 That's water under the bridge, but old habits often don't die, especially if people just adopted them without knowing the reasons. 那是桥下的水,但是旧习惯常常不会消失,尤其是如果人们只是在不知道原因的情况下采用它们的时候。

Instead of span , you could use a too. 取而代之的span ,你可以使用a了。 Without any href attribute, an a element has no special meaning. 没有任何href属性, a元素没有特殊的含义。

By nondisruptiveness principles, the element should be generated dynamically with JavaScript instead of being a static part of the page (since when JavaScript is disabled, it would just sit there, making an impression of being relevant, but without doing anything). 根据非破坏性原则,应该使用JavaScript动态生成该元素,而不是将其作为页面的静态部分(因为禁用JavaScript后,它只会放在那儿,给人以相关的印象,但无需执行任何操作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM