记录 div 部分,使用 title 属性?
我正在寻找最简洁的方式来记录页面的各个部分,例如 div 部分。
使用 title 属性有什么问题吗,例如
<div title="Payment Area" class="form">
...
</div>
?
PS 在相关的情况下,我正在使用 IntelliJ IDE,但对其各种功能不熟悉,例如自动格式控制和其他能够轻松理解页面部分的方法。
I'm looking for the most terse way to document sections of my page, e.g. div sections.
Anything wrong with using the title attribute, e.g.
<div title="Payment Area" class="form">
...
</div>
?
P.S. In case relevant, I'm using the IntelliJ IDE, but new to its various capabilities, e.g. automatic formatting control and other ways to be able to easily understand sections of my pages.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果某个元素具有
title
属性,则当您将鼠标悬停在该元素上时,大多数浏览器都会显示带有该属性值的工具提示。在这种情况下,当您将鼠标悬停在该div
上的任意位置时,它们会显示“付款区域”工具提示。一般来说,HTML 属性都有其用途。 HTML 有文档注释!尝试一下:
您提到您不想要评论的开销。如果你计算一下字符(包括属性之前所需的空格),它们实际上是相同的长度:(
但是,我同意,它们看起来确实更大!)
If an element has a
title
attribute, most browsers show a tooltip with the attribute’s value when you hover over it. In this case, they’ll show a “Payment Area” tooltip when you hover anywhere over thatdiv
.HTML attributes, in general, have a purpose. HTML has comments for documentation! Try them:
You mentioned that you didn't want the overhead of comments. If you count up the characters (including the required space before the attribute, they're actually the same length:
(But, I agree, they do look bigger!)
我可能会使用 id,这样您也可以将它们用作 CSS 和 JavaScript 的挂钩。
否则,Gabe's Answer 为
data
属性提供了一个很好的案例。 John Resig 在这里解释了为什么它们很好:http://ejohn.org/blog/ html-5-数据属性/I would probably use
id
s, that way you can also use them as hooks for your CSS and JavaScript.Otherwise, Gabe's Answer makes a good case for
data
attributes. John Resig explains why they are good here: http://ejohn.org/blog/html-5-data-attributes/我建议使用 HTML5 自定义属性标准:
基本上,在名称前面加上
data-
,浏览器将忽略它。I would recommend using the HTML5 custom attributes standard:
Basically, precede the name with
data-
and the browser will ignore it.title 属性不适用于 div - 我还没有检查官方规范,但我认为它甚至不是受支持的属性。更好的做法是使用“id”属性。这样做的另一个好处是为 CSS 提供钩子。
您还可以更进一步,使用 html5,它为您提供更多描述性元素,例如
编辑:根据 steveax 的评论,标题属性无效。不过,我认为我的回答的要点仍然有效。
The title attribute isn't meant for divs - I haven't checked the official specs, but I don't think it's even a supported attribute. The better practice is to use the "id" attribute. This has the added benefit of providing hooks for your CSS.
You can also take it a step further and use html5, which provides you with more descriptive elements such as
<nav>
,<section>
,<article>
and<header>
Edit: as per comment by steveax, the title attribute isn't invalid. I think the gist of my answer remains valid, though.
您可以使用 rel="" 属性,这对我来说效果很好,我认为标题不适用于 div
You can use the rel="" attribute, that works pretty well for me, I don't think title works on divs