为什么不“$.each”?似乎有效?

发布于 11-15 19:35 字数 2263 浏览 1 评论 0原文

当我在同一个 HTML 文档中包含以下内容时,它似乎可以工作:

<script type="text/javascript">
datasets[0] = {
    "buildnames": {
        label: "buildnames",
        data: ["test2-a1", "test2-a2", "test2-a3",  "test2-a4","test2-a5", "test2-a6"]
    }
};

function doSomething() {
    var i = 0;
    for (var j = 0; j < datasets.length; j++){
        $.each(datasets[j], function(key, val) {
            val.color = i;
            ++i;
        });

        var choiceContainer = $("#choices");
        //var testkey = new RegExp("cover");
        $.each(datasets[j], function(key, val) {
            if (key.indexOf("failed") > -1 || key.indexOf("cover") > -1) {
                choiceContainer.append('<br /><input type="checkbox" name="' + key +
                '" checked="checked" id="id' + key + '">' +
                '<label for="id' + key + '">'
                + val.label + '</label>');
            }
        });
    }
}
</script>

但是当我将其导出到单独的 JavaScript 文件,然后导入它并调用这样的函数时,

<script type="text/javascript" language="Javascript" src="root/include/graph_1.js"></script>

<a href="javascript:doSomething()">doSomething</a>

我收到以下错误:'$' 未定义并指向“$.each(..” 那么为什么我不能使用这个功能,因为我将 HTML 文档中的所有 JavaScript 代码“导出”到另一个单独的 JavaScript 文件中呢?

***********< /strong>编辑******< em>***************< em>**************

我尝试了下面的提示并更改了顺序

@idbentley我做了你所说的并更改了这两个的顺序,所以它是

    <script language="javascript" type="text/javascript" src="root/include/jquery.js"></script>
    <script language="javascript" type="text/javascript" src="root/include/jquery.flot.js"></script>
    <script language="javascript" type="text/javascript" src="root/include/jquery.flot.crosshair.js"></script>
    <script type="text/javascript" language="Javascript" src="root/include/graph_1.js"></script>

但现在我收到另一个错误“抛出异常但未捕获”有人知道这个错误是什么意思吗?

When I have the following inside the same HTML document it seems to work:

<script type="text/javascript">
datasets[0] = {
    "buildnames": {
        label: "buildnames",
        data: ["test2-a1", "test2-a2", "test2-a3",  "test2-a4","test2-a5", "test2-a6"]
    }
};

function doSomething() {
    var i = 0;
    for (var j = 0; j < datasets.length; j++){
        $.each(datasets[j], function(key, val) {
            val.color = i;
            ++i;
        });

        var choiceContainer = $("#choices");
        //var testkey = new RegExp("cover");
        $.each(datasets[j], function(key, val) {
            if (key.indexOf("failed") > -1 || key.indexOf("cover") > -1) {
                choiceContainer.append('<br /><input type="checkbox" name="' + key +
                '" checked="checked" id="id' + key + '">' +
                '<label for="id' + key + '">'
                + val.label + '</label>');
            }
        });
    }
}
</script>

But when I'm exporting this to a separate JavaScript file and then importing it and call the function like this

<script type="text/javascript" language="Javascript" src="root/include/graph_1.js"></script>

<a href="javascript:doSomething()">doSomething</a>

I get the following error: '$' is undefined and points at "$.each(.."
So why can't I use this function is I "export" all JavaScript code in an HTML document to another separate JavaScript file?

***********edit***********************************

I tried the tips below and changed the order

@idbentley I did what you said and changed the order of those 2 so it is

    <script language="javascript" type="text/javascript" src="root/include/jquery.js"></script>
    <script language="javascript" type="text/javascript" src="root/include/jquery.flot.js"></script>
    <script language="javascript" type="text/javascript" src="root/include/jquery.flot.crosshair.js"></script>
    <script type="text/javascript" language="Javascript" src="root/include/graph_1.js"></script>

But now I get another error "Exception thrown and not caught" Anyone know what this error means?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的往事2024-11-22 19:35:29

仅凭您给我们的 hwat 很难判断,但听起来您没有包含 jQuery,或者您在脚本之后加载 jQuery。

Hard to tell with just hwat you gave us, but it sounds like you aren't including jQuery, or that you are loading jQuery after your script.

蹲墙角沉默2024-11-22 19:35:29

在包含任何您自己的脚本之前,您需要包含 JQuery 库。

You need to include the JQuery library BEFORE you include any of your own scripts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文