我正在寻找好的/语义/传递验证方式来指示我的链接,这些链接用作 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!
发布评论
评论(2)
您可以使用
class
属性,或者从 HTML5 开始,使用 自定义数据属性。You may use the
class
-attribute, or as of HTML5, the custom data-attributes.如果链接是“增强”链接,则其功能与通常的链接几乎相同,但以更“用户友好”的方式 - 例如导航链接,当在页面上启用 JavaScript 时,将仅重新加载页面的“内容”部分,并使用 HTML5 History API 更新页面的地址 –那么我就使用一个语义类,实际上描述链接,例如“导航”。
如果它们是 JavaScript 触发器,并且当 JavaScript 关闭时它们将无法运行 – 我建议根本不要使用
a
元素。来自规范:我相信在大多数情况下,“操作触发器”并不真正符合
a
元素的使用描述。因此,我建议使用span
元素,该元素的样式表明它是交互的触发器。再次引用规范:关于这些触发器的另一个建议:使用这行代码(文档头部</code> 之后的第一行)来给您的 <code>html</code> 元素a class <code>js</code>:
然后在 CSS 中执行以下操作:
这样,当 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: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 aspan
element, that would be styled in a way that would suggest that it's a trigger for interaction. Quoting the spec again: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 yourhtml
element a classjs
:Then in your CSS do this:
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.