noscript 来过滤添加超链接 - 干净的 JavaScript 降级

发布于 2024-10-02 21:01:49 字数 414 浏览 0 评论 0原文

我的项目依靠 JavaScript 在超链接点击时动态显示内容。为了在不启用 JavaScript 的情况下使其完全降级,我只是显示所有页面内容并使用超链接和锚点来连接各个部分。

我依靠 jQuery 通过 ID 来识别超链接的点击,因此如果没有 JavaScript,我需要添加锚点。

这是 noscript 的好用吗?主要是,这总是会在没有 JavaScript 的情况下添加超链接吗?

<div id="link1">
  <noscript><a href="#link1content"></noscript>
    1. Link Name Here
  <noscript></a></noscript>
</div>

My project relies on JavaScript to dynamically display content on a hyperlink click. In order to make it cleanly degrade without JavaScript enabled, I'm simply showing all page content and using hyperlinks and anchors to connect the pieces.

I'm relying on jQuery to identify the click of the hyperlink by ID, so without JavaScript I need to add in the anchor.

Is this a good use of noscript? Mainly, will this always add the hyperlink without JavaScript?

<div id="link1">
  <noscript><a href="#link1content"></noscript>
    1. Link Name Here
  <noscript></a></noscript>
</div>

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

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

发布评论

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

评论(3

那伤。 2024-10-09 21:01:49

元素包含其他元素,而不仅仅是标签。开始标签和结束标签必须位于容器中。 验证者会发现这一点。

不过,您一开始就不应该为此使用 noscript。更多类似的东西:

<a class="enhanced_in_some_way" href="#link1content">1. Link Name Here</a>

jQuery('a.enhanced_in_some_way').click(function (event) {
    var link = this;
    var name = /#([^#]+$)/.exec(link.href);
    do_something_with(name);
    event.preventDefault();

});

……可能是前进的方向。

Elements contain other elements, not just tags. The start tag and the end tag must be in the container. A validator would have picked this up.

You shouldn't be using noscript for this in the first place though. Something more along the lines of:

<a class="enhanced_in_some_way" href="#link1content">1. Link Name Here</a>

with

jQuery('a.enhanced_in_some_way').click(function (event) {
    var link = this;
    var name = /#([^#]+$)/.exec(link.href);
    do_something_with(name);
    event.preventDefault();

});

… is probably the way forward.

兔姬 2024-10-09 21:01:49

在许多浏览器中,这可以工作,但我不推荐它。它完全破坏了 HTML 的结构,并且不能保证在所有浏览器中工作,甚至不能保证它现在可以工作的浏览器的未来版本。

In many browsers, this will work, but I wouldn't recommend it. It completely breaks the structure of the HTML and is not guaranteed to work in all browsers or even future versions of browsers that it does work in now.

冷夜 2024-10-09 21:01:49

实际上,这里不需要任何脚本。默认情况下,将锚点目标放入超链接中,并在 jQuery 或 javascript 单击函数中以 return falseevent.preventDefault(); 结束。如果启用 JavaScript,这将阻止链接被跟踪,如果禁用,则会导致跟踪锚点。您可以用最少的开销实现这两种方式。

Actually, you don't need noscript here. Put the anchor destination into the hyperlinks by default, and in your jQuery or javascript click function, finish with a return false or event.preventDefault();. This will prevent the link from being followed if JavaScript is enabled, and results in a followed anchor if it is disabled. You're covered both ways with minimal overhead.

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