【问题标题】:HTML table Uncaught ReferenceError: $ is not defined [duplicate]HTML table Uncaught ReferenceError: $ is not defined [重复]
【发布时间】:2021-03-29 14:07:27
【问题描述】:

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

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

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

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

我有这个项目:

网站.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

【问题讨论】:

  • 那么你是否包含了 jQuery?是&lt;script src="/lib/jquery.plugin.js"&gt;&lt;/script&gt;jquery 吗?正在加载吗?
  • 看起来您正在尝试从本地驱动器而不是通过网络服务器运行 html/js。这将在一定程度上起作用,但您很快就会遇到 CORS 和其他问题。尝试安装轻量级服务器,请参阅此处获取一些建议:stackoverflow.com/questions/5050851/…
  • 啊,不错,是的,我的 table.csv 请求确实收到了 CORS 错误,我会查看该帖子。谢谢

标签: html jquery ajax


【解决方案1】:

你缺少 Jquery,在 &lt;/body&gt; 标签之前添加这个来修复

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

【讨论】:

  • 摆脱了警告,发布更新,但现在它希望 chrome 无法加载:“file:///C:/lib/jquery.plugin.js”我猜是我需要从网上获得的文件,有网址还是我需要在本地?
猜你喜欢
  • 1970-01-01
  • 2015-04-30
  • 2015-12-24
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
  • 1970-01-01
  • 2019-01-22
  • 2019-12-31
相关资源
最近更新 更多