如何在HTML代码中注释/取消注释

vik*_*tra 28 html comments coding-style

通常在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>
Run Code Online (Sandbox Code Playgroud)

现在,如果我必须隐藏视图模板的某些部分,在php的情况下,我只需选择所需的代码并放置单行注释(大多数时候使用快捷键).

但是,在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>-->
Run Code Online (Sandbox Code Playgroud)

然后,当我完成测试时,我必须经历将那些结束标签放回去的痛苦.

在HTML中是否有更好的和省时的块注释方式?

Ala*_*ano 18

是的,要评论结构元数据,

注释掉大部分HTML(注释区块)

我在.html文件中的个人方式是打开:<script>/*并关闭它*/</script>

<script>/* hiding code go here */</script>

是问题的解决方法,因为不是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>
Run Code Online (Sandbox Code Playgroud)

在一个案例中,PHP文件中的HTML 使用注释标记<?/*<?php /*关闭它*/?>.请记住,该文件必须是.php扩展名,并且不能在.html中使用.

<?/* hiding code go here */?>

在.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>
*/?>
Run Code Online (Sandbox Code Playgroud)

值得一提的不是HTML,但一般的开发人员实践是注释掉元数据的一部分,以便它不会在浏览器中呈现和/或执行.在HTML中,注释掉多行可能非常耗时.排除包含注释,CSS或代码的模板结构元数据片段并系统地注释以查找错误的来源非常有用.对块进行注释被认为是一种不好的做法,建议使用版本控制系统.HTML4中需要属性"type",HTML5中是可选的.

  • 在.html中使用<script>/*...*/</ script> - smart !! (4认同)

Rob*_*ert 10

取决于扩展.如果是.html,您可以<?用来开始和?>结束评论.这真的是我能想到的唯一选择.http://jsfiddle.net/SuEAW/