简体   繁体   English

如何在 HTML 属性中注释?

[英]How to comment within an HTML attribute?

The final (closing) angle bracket in the code below is not interpreted as closing the <input> element:下面代码中的最后一个(结束)尖括号不被解释为结束<input>元素:

<input type='text' name='name' <!-- id='name' -->>

I thought this was a valid way to comment out this attribute but Google Chrome, Firefox and Notepad++ (color coding) all suggest that this is not the way to go.我认为这是注释掉此属性的有效方法,但 Google Chrome、Firefox 和 Notepad++(颜色编码)都表明这不是可行的方法。

I used CTRL + Shift + Q in Notepad++ to do this.我在 Notepad++ 中使用了CTRL + Shift + Q来做到这一点。

Then what is the proper way to comment out this <id> attribute?那么注释掉这个<id>属性的正确方法是什么?

HTML provides no way to place a comment inside a tag. HTML 没有提供在标签内放置注释的方法。


If you are generating the HTML from a template / programming language, then you can use features of that to comment something out.如果您是从模板/编程语言生成 HTML,那么您可以使用它的功能来注释掉某些内容。

For example, in Template-Toolkit:例如,在模板工具包中:

<input type='text' name='name' [%# id='name' %]>

or PHP:或 PHP:

<input type='text' name='name' <?php # id='name' ?>>

If you are using HTML 5 then you could (as an ugly hack) use a data attribute to "comment" out entire attributes.如果您使用的是 HTML 5,那么您可以(作为一个丑陋的黑客)使用数据属性来“注释”整个属性。

<input type='text' name='name' data-comment-id='name'>

I usually just put _x at the end of the attribute name.我通常只是将_x放在属性名称的末尾。 Then the attribute is ignored because it's unknown.然后该属性被忽略,因为它是未知的。 So if I wanted to comment out the id attribute from this element:因此,如果我想从该元素中注释掉id属性:

<input type="text" name="name" id="name">

I would change it to this:我会把它改成这样:

<input type="text" name="name" id_x="name">

This also has the advantage of being able to search for " _x= " to find all commented attributes.这还具有能够搜索“ _x= ”以查找所有注释属性的优点。

<input type='text' name='name' <?php  /* id='name' */ ?> >

您可以使用它在查看源信息时不会被解释

If you want to comment a line in HTML5 and don't use如果你想在 HTML5 中注释一行并且不要使用

<!-- Html comments -->

You can use您可以使用

  • aria-label咏叹调标签

for example:例如:

 <button aria-label="Close" nclick="myDialog.close()">X</button>

or要么

<button aria-label="This button close the popup" nclick="myDialog.close()">X</button>

Since there is nothing to indicate that the purpose of the button is to close the dialog, the aria-label attribute is used to provide a label to any assistive technology or simply comments.由于没有任何内容表明按钮的目的是关闭对话框,因此 aria-label 属性用于为任何辅助技术或简单的注释提供标签。

I agree, that you should not use comments at this place.我同意,你不应该在这个地方使用评论。 That said, the following should work in Chrome, Firefox and IE:也就是说,以下内容应该适用于 Chrome、Firefox 和 IE:

<input type="text" %id="test1"% class="test2">

在谷歌浏览器中检查元素

在 Firefox 中检查元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM