哪个“href”值应该用于JavaScript链接,“#”或“javascript:void(0)”? [英] Which "href" value should I use for JavaScript links, "#" or "javascript:void(0)"?

查看:122
本文介绍了哪个“href”值应该用于JavaScript链接,“#”或“javascript:void(0)”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是构建链接的两种方法,其中唯一目的是运行JavaScript代码。在功能,页面加载速度,验证目的等方面哪个更好?

  function myJsFunc(){alert(myJsFunc);}  

 < a href =#onclick =myJsFunc();>运行JavaScript代码< / a>  





  function myJsFunc(){alert(myJsFunc);}  

 < a href =javascript:void(0)onclick =myJsFunc();>运行JavaScript代码< / a>  

javascript:void(0)。



原因。鼓励在开发团队中使用不可避免地导致一些使用函数的返回值,如下所示:

 函数doSomething(){
//一些代码
return false; (

但是他们忘记使用 return doSomething() 在onclick中,只需使用 doSomething()



是最后的返回false;如果被调用的函数抛出一个错误,将不会执行。因此,开发人员还必须记得在被调用的函数中适当地处理任何错误。



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

  onclick =someFunc.call(this)

OR

  onclick =someFunc.apply(this,arguments)

使用 javascript:void(0)可以避免所有上述的麻烦,而且我还没有发现任何不利的例子。



所以如果你是一个独立的开发者,那么你可以明确地做出自己的选择,但是如果你以团队的身份工作,你必须声明:

使用 href =#,确保 onclick 始终包含返回false; 在最后,任何调用的函数都不会抛出错误,并且如果您动态地将函数附加到 onclick 属性上,请确保并且不会抛出错误,它会返回 false



OR



使用 href =javascript:void(0)



交流更容易。

The following are two methods of building a link that has the sole purpose of running JavaScript code. Which is better, in terms of functionality, page load speed, validation purposes, etc.?

function myJsFunc() {
    alert("myJsFunc");
}

<a href="#" onclick="myJsFunc();">Run JavaScript Code</a>

or

function myJsFunc() {
    alert("myJsFunc");
}

 <a href="javascript:void(0)" onclick="myJsFunc();">Run JavaScript Code</a>

解决方案

I use javascript:void(0).

Three reasons. Encouraging the use of # amongst 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 much easier to communicate.

这篇关于哪个“href”值应该用于JavaScript链接,“#”或“javascript:void(0)”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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