【问题标题】:Javascript: Uncaught SyntaxError: Unexpected token <Javascript:未捕获的 SyntaxError:意外的令牌 <
【发布时间】:2016-10-23 05:21:35
【问题描述】:

我有一个名为 table_java.js 的 js 文件,我正试图让它在我的 HTML 文件 test.html 中工作。 我不断收到错误,Uncaught SyntaxError: Unexpected token &lt; 我认为问题在于 js 文件被作为文本文件读取。但我不确定这是否是问题/不知道如何解决它。任何帮助/建议将不胜感激。

这里是 table_java.js

<script type ="text/javascript">
<!--
        $(document).ready( function () {

                // Setup - add a text input to each footer cell
                $('#POGs_table tfoot th').each( function () {
                    var title = $(this).text();
                    $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
                } );

            var table = $('#POGs_table').DataTable( {
                "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
                "order": [[0,"asc"]],
                "aoColumnDefs": [ { "sSortDataType": "numeric", "aTargets": [3,4,5] } ]
            } );


                // Apply the search
                table.columns().every( function () {
                    var that = this;

                    $( 'input', this.footer() ).on( 'keyup change', function () {
                            if ( that.search() !== this.value ) {
                                that
                                        .search( this.value )
                                        .draw();
                            }
                    });
                });
            //Hide/Show function
            $('a.showHideColumn').on('click', function() {

                var tableColumn = table.column($(this).attr('data-columnindex'));
                tableColumn.visible(!tableColumn.visible());
            });
        });
-->
</script>

这里是 test.html 的头部

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Phage Orthologous Groups</title>
    <link rel="stylesheet" type="text/css" href="http://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css">
    <link rel="stylesheet" type="text/css" href="krist_datatables.css">
    <link rel="stylesheet" type="text/css" href="table_try.css">
    <script type="text/javascript" charset="utf8" src="http://code.jquery.com/jquery-1.12.0.min.js"></script>
    <script type="text/javascript" src="http://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
    <script  type="text/javascript" src="table_java.js"></script>
</head>

【问题讨论】:

  • 脚本中不需要 &lt;!----&gt;script 标记

标签: javascript jquery datatables


【解决方案1】:

从 table_java.js 中移除标签,它已经是 JS 脚本了

【讨论】:

  • 同时删除开头的 。这些用于 HTML cmets。
【解决方案2】:

--&gt; 不是有效的 JavaScript。必须转义://--&gt;

现在您不需要脚本块中的 html cmets。任何仍在使用古老到不支持 &lt;script&gt; 的浏览器的人都是恐龙,您无需担心支持。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-19
    • 2016-04-04
    • 2013-10-08
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    相关资源
    最近更新 更多