Html 记录 div 部分,使用标题属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7439250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 10:38:28  来源:igfitidea点击:

documenting div sections, use title attribute?

htmlxhtml

提问by Ray

I'm looking for the most terse way to document sections of my page, e.g. div sections.

我正在寻找最简洁的方式来记录我的页面部分,例如 div 部分。

Anything wrong with using the title attribute, e.g.

使用 title 属性有什么问题,例如

<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.

PS 如果相关,我使用的是 IntelliJ IDE,但对它的各种功能不熟悉,例如自动格式化控制和其他能够轻松理解我的页面部分的方法。

采纳答案by Jason Gennaro

I would probably use ids, that way you can also use them as hooks for your css and javascript.

我可能会使用ids,这样您也可以将它们用作 css 和 javascript 的钩子。

Otherwise, @Gabe makes a good case for dataattributes. John Resig explains why they are good here: http://ejohn.org/blog/html-5-data-attributes/

否则,@Gabe 是一个很好的data属性案例。John Resig 解释了为什么他们在这里很好:http: //ejohn.org/blog/html-5-data-attributes/

回答by s4y

If an element has a titleattribute, 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 anywhereover that div.

如果一个元素有一个title属性,当你将鼠标悬停在它上面时,大多数浏览器会显示一个带有属性值的工具提示。在这种情况下,他们会显示“支付区”提示,当你悬停在任何地方在那div

HTML attributes, in general, have a purpose. HTML has comments for documentation! Try them:

一般来说,HTML 属性是有目的的。HTML 有文档注释!试试看:

<div class="form"> <!-- Payment Area -->
    ...
</div>

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:

你提到你不想要评论的开销。如果你计算字符(包括属性前所需的空格,它们实际上是相同的长度:

 title="Payment Area"
<!-- Payment Area -->

(But, I agree, they do look bigger!)

(但是,我同意,它们确实看起来更大!)

回答by Gabe

I would recommend using the HTML5 custom attributes standard:

我建议使用 HTML5 自定义属性标准:

<div data-title='Payment Area'></div>

Basically, precede the name with data-and the browser will ignore it.

基本上,在名称前面加上data-,浏览器会忽略它。

回答by Philip Schweiger

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.

title 属性不适用于 div - 我没有检查过官方规范,但我认为它甚至不是受支持的属性。更好的做法是使用“id”属性。这具有为您的 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>

你也可以把它更进一步,使用HTML5,它为您提供了更具描述性的元素,例如<nav><section><article><header>



Edit: as per comment by steveax, the title attribute isn't invalid. I think the gist of my answer remains valid, though.

编辑:根据 steveax 的评论,title 属性无效。不过,我认为我的答案的要点仍然有效。

回答by Reina

You can use the rel="" attribute, that works pretty well for me, I don't think title works on divs

您可以使用 rel="" 属性,这对我来说效果很好,我认为标题不适用于 div

<div rel="Payment Area"></div>