简体   繁体   English

为什么使用'href =“javascript:void(0);”'而不是更友好的用户?

[英]Why use 'href=“javascript:void(0);”' instead of something more user friendly?

I'm new to web-design and javascript, and I don't understand why it's common for web designers to use the void(0) syntax below: 我是网页设计和javascript的新手,我不明白为什么网页设计师使用下面的void(0)语法是常见的:

 <a onclick="this.blur();return false;" href="javascript:void(0);" class="btn">

Since this actually shows itself in the lower left hand corner of the browser, why not come up with a way to make it more user friendly? 由于这实际上显示在浏览器的左下角,为什么不提出一种方法使其更加用户友好? Maybe it's because designers don't have the time to do this kind of UI tweaking. 也许是因为设计师没有时间做这种UI调整。

What would you suggest as an alternative to the above syntax that would be more helpful to the end-user? 作为上述语法的替代方案,您会建议哪些对最终用户更有帮助?

The ideal solution is to build on things that work . 理想的解决方案是建立有用的东西

This article discusses progressive enhancement leveraging the Yahoo User Interface Library javascript library to improve accessibility of javascript simulated interaction events. 本文讨论了利用Yahoo用户界面库javascript库来改进javascript模拟交互事件的可访问性的渐进增强。

cite: Christian Heilmann 引用:Christian Heilmann

If it's simply end-user helpfulness then this might do? 如果它只是最终用户的乐于助人,那么这可能会有用吗?

<a onclick="this.blur();return false;" href="#Click to hide" class="btn">

since you're returning false anyway it could actually contain anything 既然你还在返回假,它实际上可以包含任何东西

<a onclick="this.blur();return false;" href="Click to hide" class="btn">

However, I think the reason for it is, if you have javascript disabled, it will actually have no effect, as opposed to writing something else there which will (the first example will change the hash/anchor-part of the url, the second will probably result in an error). 但是,我认为它的原因是,如果你禁用了javascript,它实际上没有任何效果,而不是在那里写一些其他东西(第一个例子将改变url的hash / anchor-part,第二个可能会导致错误)。 Either way they'll still look funny though (prepended with the original url). 无论哪种方式,他们仍然看起来很有趣(加上原始网址)。

Then again, if this thing is only visible due to javascript, I'd say go ahead and assume javascript is available. 再说一次,如果这个东西只是由于javascript可见,我会说继续并假设javascript可用。 Although I'd still err on the safe side and use the hash anyway... :) 虽然我仍然在安全方面犯错并且仍然使用哈希... :)

Edit : 编辑
I just realized not all browsers show the '#' part of a href in the status-bar (does any browser do that?), so I'd use the javascript:void(0); 我刚刚意识到并非所有浏览器都在状态栏中显示href的'#'部分(任何浏览器都这样做吗?),所以我使用javascript:void(0); or simply # if you're not using anchors for anything, and set the title attribute to something descriptive instead. 或者只是#如果你没有使用任何东西的锚点,并将title属性设置为描述性的东西。

Edit : 编辑
I think I was a bit too fast... I think the javascript:void is indeed to avoid having any effect. 我觉得我有点太快......我认为javascript:void确实是为了避免产生任何影响。 However, a better solution would to not use a -tags at all (I usually just slap the onclick onto anything, actually I usually use jquery, and add it to certain classes or IDs afterwards). 然而,一个更好的解决办法不使用a在所有标签都有效(我通常只是一巴掌的onclick什么嘛,其实我平时使用jquery,并将其添加到某些类或ID之后)。

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

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