如何在 HTML 代码中注释/取消注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3757051/
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
How to comment/uncomment in HTML code
提问by vikmalhotra
Often while coding view templates in html, my habit of adding some helpful comments causes lots of time-consuming effort while testing.
通常在 html 中编码视图模板时,我添加一些有用的注释的习惯会在测试时导致大量耗时的工作。
Consider this code...
考虑这个代码...
<!-- Here starts the sidebar -->
<div id="sidebar">
....
</div>
<!-- Here starts the main contents pane -->
<div id="main-contents">
...
</div>
<!-- Here starts the footer -->
<div id="footer">
...
</div>
Now, if I have to hide out some portion of the view template, in case of php I would just select the desired code and put single-line comments (using a shortcut key most of the times).
现在,如果我必须隐藏视图模板的某些部分,在 php 的情况下,我只会选择所需的代码并放置单行注释(大多数情况下使用快捷键)。
However, in html code, where only the block comments work, I end-up removing all the closing comment tags (-->) till the position I want the commenting to occur - something like this...
但是,在 html 代码中,只有块注释起作用,我最终删除了所有结束注释标记 (-->) 直到我希望注释发生的位置 - 像这样......
<!-- Here starts the sidebar
<div id="sidebar">
....
</div>
<!-- Here starts the main contents pane
<div id="main-contents">
...
</div>
<!-- Here starts the footer
<div id="footer">
...
</div>-->
Then when I'm done testing I have to go through the agony of putting back those closing tags.
然后当我完成测试时,我必须经历把那些结束标签放回去的痛苦。
Is there a better and time saving way of block commenting in HTML?
有没有更好更省时的方式来阻止 HTML 中的注释?
采纳答案by Robert
Depends on the extension. If it's .html, you can use <?
to start and ?>
to end a comment. That's really the only alternative that I can think of. http://jsfiddle.net/SuEAW/
取决于扩展名。如果是 .html,您可以使用<?
开始和?>
结束评论。这真的是我能想到的唯一选择。http://jsfiddle.net/SuEAW/
回答by Alan Mattano
Yes, to comment structural metadata out,
是的,要注释掉结构元数据,
Comment out large sections of HTML (Comment Out Block)
注释掉大部分 HTML(注释块)
my personal way in a .htmlfile is opening: <script>/*
and close it with */</script>
我在.html文件中的个人方式是open: <script>/*
and close it with*/</script>
<script>/* hiding code go here */</script>
<script>/* hiding code go here */</script>
Is a workaround to the problem since is not HTML.
是该问题的解决方法,因为它不是 HTML。
Considering your code in .html...
考虑您的 .html 代码...
<!-- Here starts the sidebar -->
<div id="sidebar">
....
</div>
<script>/*
<!-- Here starts the main contents pane -->
<div id="main-contents">
...
</div>
<!-- Here starts the footer -->
<div id="footer">
...
</div>
*/</script>
And in a case is HTML inside PHPfile using comment tag <?/*
or <?php /*
and close it with */?>
. Remember that the file must be .php extension and don't work in .html.
在一种情况下是使用注释标记的PHP文件中的HTML<?/*
或<?php /*
使用*/?>
. 请记住,该文件必须是 .php 扩展名,不能在 .html 中工作。
<?/* hiding code go here */?>
<?/* hiding code go here */?>
Considering your code in .php...
考虑您在 .php 中的代码...
<!-- Here starts the sidebar -->
<div id="sidebar">
....
</div>
<?/*
<!-- Here starts the main contents pane -->
<div id="main-contents">
...
</div>
<!-- Here starts the footer -->
<div id="footer">
...
</div>
*/?>
Is worth nothing that is not HTML but a common developer practice is to comment out parts of metadata so that it will not be rendered and/or executed in the browser. In HTML, commenting out multiple lines can be time-consuming. It is useful to exclude pieces of template structural metadata containing comments, CSS or code and systematically commenting out to find the source of an error. It is considered a bad practice to comment blocks out and it is recommended to use a version control system. The attribute "type" is required in HTML4 and optional in HTML5.
除了 HTML 之外没有任何价值,但一个常见的开发人员做法是注释掉元数据的一部分,这样它就不会在浏览器中呈现和/或执行。在 HTML 中,注释掉多行可能很耗时。排除包含注释、CSS 或代码的模板结构元数据片段并系统地注释掉以查找错误源非常有用。注释块被认为是一种不好的做法,建议使用版本控制系统。属性“type”在 HTML4 中是必需的,在 HTML5 中是可选的。
回答by u11
you can try to replace -->
with a different string say, #END#
and do search and replace with your editor when you wish to return the closing tags.
您可以尝试-->
用不同的字符串替换,例如,#END#
当您希望返回结束标签时,使用您的编辑器进行搜索和替换。
回答by user3178029
Put a space between the "-->" of your header comments. e.g. "- ->"
在标题注释的“-->”之间放置一个空格。例如“-->”
回答by whatnick
I find this to be the bane of XML style document commenting too. There are XML editors like eclipse that can perform block commenting. Basically automatically add extra per line and remove them. May be they made it purposefully hard to comment that style of document it was supposed to be self explanatory with the tags after all.
我发现这也是 XML 样式文档注释的祸根。有像 eclipse 这样的 XML 编辑器可以执行块注释。基本上每行自动添加额外并删除它们。可能是他们故意使评论这种风格的文档变得困难,毕竟它应该是带有标签的自我解释。
回答by vikmalhotra
My view templates are generally .php files. This is what I would be using for now.
我的视图模板通常是 .php 文件。这就是我现在要使用的。
<?php // Some comment here ?>
The solution is quite similar to what @Robert suggested, works for me. Is not very clean I guess.
该解决方案与@Robert 建议的非常相似,对我有用。我猜是不是很干净。
回答by scrblnrd3
Eclipse Juno has a good way for it. You just do the cmd+/
Eclipse Juno 有一个很好的方法。你只要做cmd+/
回答by dstrong
The following works well in a .php file.
以下在 .php 文件中运行良好。
<php? /*your block you want commented out*/ ?>
回答by David
No. Unless you find a tool that does what you described for you.
不可以。除非您找到了一种工具可以满足您的描述。
回答by Jeff Knecht
Depending on your editor, this should be a fairly easy macro to write.
根据您的编辑器,这应该是一个相当容易编写的宏。
- Go to beginning of line or highlighted area
- Insert <!--
- Go to end of line or highlighted area
- Insert -->
- 转到行首或突出显示的区域
- 插入 <!--
- 转到行尾或突出显示的区域
- 插入 -->
Another macro to reverse these steps, and you are done.
另一个宏来反转这些步骤,你就完成了。
Edit: this simplistic approach does not handle nested comment tags, but should make the commenting/uncommenting easier in the general case.
编辑:这种简单的方法不处理嵌套的注释标签,但在一般情况下应该使注释/取消注释更容易。