【问题标题】:Using comments in script link tag在脚本链接标签中使用注释
【发布时间】:2012-05-18 12:56:34
【问题描述】:

在调用外部脚本的脚本标签中放置 cmets 会导致哪些问题?我的同事告诉我这不是一个好习惯。

类似这样的:

<script src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js" type="text/javascript">
            //    import jQuery
            //    cdn refers to a remotely hosted library
</script>

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    没有错,也许是可读性。 内容也将被源覆盖。

    <!-- import jQuery. CDN refers to a remotely hosted library -->
    <script src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js" type="text/javascript"></script>
    

    【讨论】:

      【解决方案2】:

      完全没有问题。如果script 元素具有src 属性,则内容将被忽略。

      也许您的同事指的是脚本标签中的 HTML cmets,它用于不支持 JavaScript 的古老浏览器?

      <script>
         <!--
            // JS was here
         // -->
      </script>
      

      Are HTML comments inside script tags a best practice?

      【讨论】:

        【解决方案3】:

        也许你的同事担心你会养成这样做的习惯

        <script src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js" type="text/javascript">
                    //    import jQuery
                    //    cdn refers to a remotely hosted library
        </script>
        

        然后最终很难弄清楚为什么这不起作用

        <script src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js" type="text/javascript">
             $(document).ready(function() {
                   $('#ActionButton').click(DoAction);
             });
        </script>
        

        因为你养成了一个坏习惯

        【讨论】:

          猜你喜欢
          • 2017-05-03
          • 1970-01-01
          • 2020-10-10
          • 1970-01-01
          • 1970-01-01
          • 2012-10-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多