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

发布于 2024-09-16 22:55:58 字数 279 浏览 3 评论 0原文

我是网页设计和 javascript 的新手,我不明白为什么网页设计者通常使用下面的 void(0) 语法:

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

既然这实际上显示在浏览器的左下角,为什么不呢想出一种方法使其更加用户友好吗?也许是因为设计师没有时间进行这种 UI 调整。

您建议什么作为上述语法的替代方案,对最终用户更有帮助?

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:

 <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.

What would you suggest as an alternative to the above syntax that would be more helpful to the end-user?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

屋顶上的小猫咪 2024-09-23 22:55:58

理想的解决方案是构建有效的东西

本文讨论了利用 Yahoo 用户界面库 javascript 库的渐进式增强,以提高 javascript 模拟交互事件的可访问性。

引用:Christian Heilmann

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.

cite: Christian Heilmann

尾戒 2024-09-23 22:55:58

如果这只是最终用户的帮助,那么这可能有用吗?

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

因为你无论如何都会返回 false 它实际上可能包含任何内容

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

但是,我认为其原因是,如果你禁用了 javascript,它实际上不会产生任何效果,而不是在那里编写其他内容(第一个示例将改变url 的哈希/锚点部分,第二个可能会导致错误)。不管怎样,它们看起来仍然很有趣(前面加上原始网址)。

话又说回来,如果这个东西只是由于 javascript 才可见,我会说继续假设 javascript 可用。尽管为了安全起见,我仍然会犯错误并使用哈希值......:)

编辑
我刚刚意识到并非所有浏览器都会在状态栏中显示 href 的“#”部分(有浏览器这样做吗?),所以我会使用 javascript:void(0); 或如果您不使用锚点进行任何操作,只需 # 即可,并将 title 属性设置为描述性内容。

编辑
我认为我有点太快了...我认为 javascript:void 确实是为了避免产生任何影响。然而,更好的解决方案是根本不使用 a 标签(我通常只是将 onclick 放在任何东西上,实际上我通常使用 jquery,然后将其添加到某些类或 ID)。

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). 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. 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); or simply # if you're not using anchors for anything, and set the title attribute to something descriptive instead.

Edit:
I think I was a bit too fast... I think the javascript:void is indeed to avoid having any effect. 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文