HTML table Uncaught ReferenceError: $ is not defined

时间:2021-03-29 14:07:27

标签: html jquery ajax

我是 HTML 的 JavaScript 方面的新手,我一直在尝试将其他一些堆栈溢出帖子整合到一起工作。

Chrome 无法加载:“file:///C:/lib/jquery.plugin.js”这是一个在线文件,还是我在本地需要的文件?

我不知道我是否丢失了文件,或者它是否无法读取文件(它们都彼此相邻)

我一直在尝试使用 chrome 开发工具来弄清楚它发生了什么。

我有这个项目:

site.html

 <head>
    <script
  src="https://code.jquery.com/jquery-3.6.0.min.js"
  integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
  crossorigin="anonymous"></script>
    <script src="/lib/jquery.plugin.js"></script>
    <script src="table.js"></script>
</head>
<table class="table table-hover">
    <thead>
        <tr>
            <th>hats</th>
            <th>boots</th>
            <th>dollars</th>
            <th>tea</th>
        </tr>
    </thead>
    <tbody>

    </tbody>
</table>

table.js

$(document).ready(function() {

    promise = $.ajax({
        type:"GET",
        dataType:"text",
        url:"table.csv",
        cache:false
    });

    promise.done(function(data){

        //Parse CSV File
        //split on new line
        var dataArr = data.split("\n");

        //for each line in array
        $.each(dataArr,function(){
            if (this != "") {

                //split files and create row
                var row = new String("");
                valArr = this.split(",");
                    row += "<tr>"

                $.each(valArr, function(){
                    row += "<td>" + this +"</td>"
                });     

                    row += "</tr>"

                    //Add row to table
                    $('tbody').append(row);

            }

        });

    });

    // Run script if request fails
    promise.fail(function() {
       console.log('A failure ocurred');
    });

});

table.csv

tophat,hiking,12,green
birthday,snow,400,english

1 个答案:

答案 0 :(得分:0)

您缺少 Jquery,请在 </body> 标记之前添加此标记以修复

 <script
  src="https://code.jquery.com/jquery-3.6.0.min.js"
  integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
  crossorigin="anonymous"></script>