noscript 来过滤添加超链接 - 干净的 JavaScript 降级
我的项目依靠 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
元素包含其他元素,而不仅仅是标签。开始标签和结束标签必须位于容器中。 验证者会发现这一点。
不过,您一开始就不应该为此使用 noscript。更多类似的东西:
与
……可能是前进的方向。
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:
with
… is probably the way forward.
在许多浏览器中,这可以工作,但我不推荐它。它完全破坏了 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.
实际上,这里不需要任何脚本。默认情况下,将锚点目标放入超链接中,并在 jQuery 或 javascript 单击函数中以
return false
或event.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
orevent.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.