锚标签的正确使用是什么?

发布于 2024-10-02 18:54:21 字数 469 浏览 2 评论 0原文

我已经读过锚标记是如何神圣的,它不应该与 javascript 一起使用:

Popup

它应该用于指向另一个页面的链接:

带我去那里

所以javascript中锚标记的正确用法是什么?我应该使用:

Energize!

还是其他变体?我对这个问题的不同观点感到有些困惑。如果将 href 制作为 JavaScript 片段,我是否只需要担心 SEO?或者这更像是一个适当的网络标准合规协议?

想法?希望我不是唯一一个感到困惑的人。

I've read how the anchor tag is holy, it should not be used with javascript:

<a href="javascript:(alert('no!'))">Popup</a>

that it should ONLY be used for a link to another page:

<a href="/grassIsAlwaysGreener.html">Take me over there</a>

So what is the proper use of the anchor tag with javascript? Should I be using:

<a href="#" onclick="someJsFn();">Energize!</a>

or some other variant? I'm somewhat confused by different views on the subject. Also is it only SEO that I should be worried about if making the href a javascript piece? Or is it more of a proper web standards compliance deal?

Thoughts? Hopefully I'm not the only one confused.

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

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

发布评论

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

评论(4

木槿暧夏七纪年 2024-10-09 18:54:21

你并不孤单,雅库布;即使是最大的 WWW 公司也使用不同的方法。

然而,根据 Netscape 时代以来的经验,我不会使用 :

<a href="javascript:(alert'no!')">Popup</a>

,这可能会在某些浏览器上造成一些麻烦,例如打开空页面或破坏当前页面上的事件顺序。

然而;

<a href="#" onclick="someJsFn();">Energize!</a>

或者;

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

不会造成严重麻烦,可以使用。请注意,前一个可能会将滚动重置到顶部。

您应该尽可能使用有意义的链接目标和不引人注目的 JavaScript,但这在现实生活中的示例中并不总是可行。它不是一个定义的标准,而是大多数 Web 开发人员高度认可的方法。

当谈到标准时,有一个与这种情况相关的标准:

您应该考虑使用“按钮”进行输入,它并不真正将访问者发送到页面,而是执行操作。这对于 SEO 也很重要。

You are not alone Jakub; even the biggest WWW companies use different approaches.

However based on experiences since Netscape days I wouldn't use :

<a href="javascript:(alert'no!')">Popup</a>

which can make some troubles on some browsers, like opening an empty page or breaking the event order on the current page.

However;

<a href="#" onclick="someJsFn();">Energize!</a>

or;

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

don't make a serious trouble and are ok to use. Note that the prior one may reset the scroll to the top.

You should use meaningful link targets and unobtrusive javascript wherever possible, but this is not always possible in real life examples. It's not a defined standard, but a method highly agreed by most of the web developers.

When it comes to standards, there is one related with this situation:

You should consider using a 'button' for inputs which doesn't really send the visitor to a page, but does an operation. This is also important for SEO.

可是我不能没有你 2024-10-09 18:54:21

正如 @Sime 所说(这确实应该是一个答案),现在在任何 HTML 对象中直接引用 javascript 被认为是“不好的做法”。因此,在这些情况下,您可以使用“unobtrusive javascript”中列出的概念,使用 jQuery 之类的东西附加事件。

正如您提到的另一个考虑因素是搜索引擎优化和可访问性。如果 SEO 对您的网站很重要,请确保仅使用标准链接即可完全导航该网站。同样,您可以使用“unobtrusive javascript”等来管理它。

As @Sime says (and it should be an answer really), it is considered "bad practise" to now directly reference javascript in any HTML object. So in these cases you attach the event using something like jQuery using the concepts laid out in "unobtrusive javascript".

As you mention another consideration is SEO and accessibility. If SEO is important to your site, make sure that the site is fully navigable using just standard links. Again you can manage this using "unobtrusive javascript", etc.

甲如呢乙后呢 2024-10-09 18:54:21

我总是像平常一样使用锚点(即指定一个备用网址,该网址是用户可以通过 javascript 执行操作的另一个位置,或者使用 javascript:void() / #) 然后使用 onclick 事件执行任何您想要执行的操作。

如果您担心一致性,您也可以使用 ,只需要对其进行样式设置(更改光标,也许还可以更改颜色)以使其在视觉上显而易见它是一个动作。

我认为 Facebook 是最好的例子。他们几乎所有的链接都绑定了 JavaScript,但他们也有一个“备份”页面,供那些不允许使用 JavaScript 或没有 JavaScript 的页面使用(后者在当今时代已经不太常见了)。看看一个模块的反应是否符合您的要求,看看他们是如何做到的。他们还投入了大量的工作来研究您可以从中受益的最佳实践。

I've always gone with using an anchor as normal (i.e. specify either an alternate url that is another location where the user could perform what's being done through javascript, or use javascript:void() / #) then use the onclick event for anything you want executed.

You could also use a <span> if you're that worried about conformance, just would need to perhaps style it (change cursor, perhaps color as well) to make it visually obvious you're making it an action.

I think Facebook is the best-case example. Almost all of their links are javascript tied in, but they also have a "backup" page for those that either have disallowed javascript or don't have it (the later, in this day and age, being far less common). Take a look at a module that reacts like you'd like yours to and see how they've done it. They also invested a bunch of work in best-practices that you can benefit from.

知足的幸福 2024-10-09 18:54:21

如果有的话,您应该仅使用像 Paul 提到的不显眼的 javascript 将锚链接绑定到 javascript 方法。

这意味着,使用关注点分离并将标记保留为 html 标记:

<a id="Jolter">Energize!</a>

以及稍后的内容

<script type="text/javascript">
    $(document).ready(function(){
        $("#Jolter").click(function(){
            // doStuffHere ...
        });
    });
</script>

If anything, you should bind your anchor links to javascript methods only by using unobtrusive javascript like Paul mentioned.

This means, using separation of concerns and leaving your markup being just that, html markup:

<a id="Jolter">Energize!</a>

and later

<script type="text/javascript">
    $(document).ready(function(){
        $("#Jolter").click(function(){
            // doStuffHere ...
        });
    });
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文