How to comment within an HTML attribute?

寵の児 提交于 2019-12-07 01:40:26

问题


The final (closing) angle bracket in the code below is not interpreted as closing the <input> element:

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

I used CTRL+Shift+Q in Notepad++ to do this.

Then what is the proper way to comment out this <id> attribute?


回答1:


HTML provides no way to place a comment inside a tag.


If you are generating the HTML from a template / programming language, then you can use features of that to comment something out.

For example, in Template-Toolkit:

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

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

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



回答2:


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

you may use this it will not be interpreted when viewing the source info




回答3:


I usually just put _x at the end of the attribute name. Then the attribute is ignored because it's unknown. So if I wanted to comment out the id attribute from this element:

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




回答4:


I agree, that you should not use comments at this place. That said, the following should work in Chrome, Firefox and IE:

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



来源:https://stackoverflow.com/questions/14020482/how-to-comment-within-an-html-attribute

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!