如何指示js使用的链接作为hook?或者 rel="js"语义替换

发布于 2024-11-30 15:54:32 字数 354 浏览 1 评论 0 原文

我正在寻找好的/语义/传递验证方式来指示我的链接,这些链接用作 javascript 的钩子。

<a href="somelink" rel="js">link</a>

据我了解,“rel”更多的是关于文档之间的关系。另外,“data-lang”(从这里)感觉不是一个足够好的解决方案。

预先非常感谢!

I'm looking for good/semantic/passing-validation way to indicate my links, that are used as hooks for javascript.

<a href="somelink" rel="js">link</a>

As I understand "rel" is more about relationships between documents. Also the "data-lang" (from here) doesn't feel to be a good enough solution.

Thanks a lot in advance!

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

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

发布评论

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

评论(2

路弥 2024-12-07 15:54:32

您可以使用 class 属性,或者从 HTML5 开始,使用 自定义数据属性

You may use the class-attribute, or as of HTML5, the custom data-attributes.

岁月蹉跎了容颜 2024-12-07 15:54:32

如果链接是“增强”链接,则其功能与通常的链接几乎相同,但以更“用户友好”的方式 - 例如导航链接,当在页面上启用 JavaScript 时,将仅重新加载页面的“内容”部分,并使用 HTML5 History API 更新页面的地址 –那么我就使用一个语义类,实际上描述链接,例如“导航”。

如果它们是 JavaScript 触发器,并且当 JavaScript 关闭时它们将无法运行 – 我建议根本不要使用 a 元素。来自规范

如果a元素具有href属性,那么它代表一个超链接
(超文本锚)。如果 a 元素没有 href 属性,则
元素代表链接可能具有的占位符
已放置(如果相关的话)。

我相信在大多数情况下,“操作触发器”并不真正符合 a 元素的使用描述。因此,我建议使用 span 元素,该元素的样式表明它是交互的触发器。再次引用规范

span 元素本身没有任何意义,但可能很有用
与全局属性一起使用时,例如 classlang
目录。它代表它的孩子。

关于这些触发器的另一个建议:使用这行代码(文档头部 </code> 之后的第一行)来给您的 <code>html</code> 元素a class <code>js</code>:

<script>document.documentElement.className = 'js';</script>

然后在 CSS 中执行以下操作:

/* 
assuming that this is your mark-up for the pseudo-links:
<span class="action-trigger">Beautiful transition</span>
*/
.action-trigger {
    display:none;
    visibility:hidden;
}
.js .action-trigger {
    display:inline;/* or whatever suits your styling of these elements */
    visibility:visible;
}

这样,当 JavaScript 被禁用时,用户将不会看到需要操作但实际上不起作用的“伪链接”。

UPD。从语义上讲,在某些情况下,您还可以使用表单提交元素,例如

If the link is an "enhanced" link, that will do pretty much the same that the usual link would do but in a more "user-friendly" way – such as a navigation link, that, when JavaScript is enabled on the page, will reload only the "content" part of the page and update the address of the page with HTML5 History API – then I would just use a semantic class, actually describing the links, such as "navigation".

In case they are JavaScript triggers and they wouldn't be functioning when JavaScript is off – I would suggest not using the a element at all. From the spec:

If the a element has an href attribute, then it represents a hyperlink
(a hypertext anchor). If the a element has no href attribute, then the
element represents a placeholder for where a link might otherwise have
been placed, if it had been relevant.

I believe that in most cases "action triggers" aren't really fitting the description of the use of an a element. Therefore, I would suggest using a span element, that would be styled in a way that would suggest that it's a trigger for interaction. Quoting the spec again:

The span element doesn't mean anything on its own, but can be useful
when used together with the global attributes, e.g. class, lang, or
dir. It represents its children.

Another suggestion regarding these triggers: use this line of code (the very first thing after the <title> in the head of your document) in order to give your html element a class js:

<script>document.documentElement.className = 'js';</script>

Then in your CSS do this:

/* 
assuming that this is your mark-up for the pseudo-links:
<span class="action-trigger">Beautiful transition</span>
*/
.action-trigger {
    display:none;
    visibility:hidden;
}
.js .action-trigger {
    display:inline;/* or whatever suits your styling of these elements */
    visibility:visible;
}

This way, when JavaScript is disabled, users won't see "pseudo-links" that would be calling for action, but wouldn't actually work.

UPD. Semantically, in certain cases, you could also use form submit elements, such as <button>: as an example – you may trigger form submission – all the "voting"/"liking"/"deleting" functionality falls into this category. Which, when JS is disabled would really submit that form, but when JS is enabled that would trigger an action on the side of your back-end API.

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