简体   繁体   English

如何在HTML源代码中注释HTML标记属性?

[英]How to comment HTML tag attribute in HTML source code?

例如:

<a /*title="need to be comment out"*/>a link</a>

The W3C documentation suggests it cannot be done: W3C文档表明无法完成:

Note that comments are markup. 请注意,注释是标记。

This basically means that a <!-- ...> comment tag is just like any other tag, so <a <!--title="need to be comment out"-->>a link</a> is as wrong as <a <span></span>>a link</a> . 这基本上意味着<!-- ...>评论标记就像任何其他标记一样,因此<a <!--title="need to be comment out"-->>a link</a>就像错误的<a <span></span>>a link</a>

For quick hacking I believe a common option is to rename that attribute. 对于快速黑客攻击,我认为一个常见的选择是重命名该属性。 While you obtain invalid HTML you can temporarily remove the attribute: 获取无效HTML时,您可以暂时删除该属性:

<a xtitle="need to be comment out">a link</a>

If you happen to be using a server side language, you can also use its own comment syntax. 如果您碰巧使用服务器端语言,您也可以使用自己的注释语法。 For instance, in PHP you can do this: 例如,在PHP中,您可以这样做:

<a <?php/*title="need to be comment out"*/?>>a link</a>

... which generates this HTML: ...生成此HTML:

<a >a link</a>

... and in ASP.NET you can use <%-- Comment goes here --%> while the MVC Razor syntax is @* Comment goes here *@ ...在ASP.NET中你可以使用<%-- Comment goes here --%>而MVC Razor语法是@* Comment goes here *@

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= ”以查找所有注释属性的优点。

You can't. 你不能。 Comments can only start and end outside tags. 注释只能在标签外部开始和结束。

Some people preprend an x to an attribute name, thus changing it and causing it to be all but ignored (since it is still often visible in the DOM), but this is invalid. 有些人将x预先发送到属性名称,从而更改它并导致它被忽略(因为它仍然经常在DOM中可见),但这是无效的。

This cannot be done, but an attribute can be removed via the removeAttribute(attribute_name) javascript call. 这不能完成,但可以通过removeAttribute(attribute_name) javascript调用删除removeAttribute(attribute_name)

Alternatively, you can prefix the attributes you want removed with a namespace like <a nosuchns:title="nevershown">click</a> and remove the namespace via javascript. 或者,您可以使用命名空间(如<a nosuchns:title="nevershown">click</a>要删除的属性添加前缀,并通过javascript删除命名空间。

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

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