JQuery从本地

时间:2015-09-18 11:06:43

标签: jquery chart.js

这说起来有点复杂,但希望你能抓住我的漂移。 (手指交叉)

我试图从名为global var的文件中调用JQuery中的data.js,该文件包含chart.js的数据列表

在我的data.js列表中,我有data_china,另一个像data_UK。您从传递到该页面的var中调用该特定data_(name)。并将global var应用于local var,以便我稍后可以将其称为chart.js

希望我的代码能够解决一些困惑。

我要问的是可以像我一样调用全局变量,还是有更有效的方法来实现它?

代码

            $.getScript("js/data.js" )
              .done(function() {
                  var name = "data_" + urlpar.toLowerCase() + "";
                  var data = name;
                  alert("Data name is:" + data + "")
              })
              .fail(function() {

                var data = {
                labels: ["FR", "Fl", "FC", "P", "TR", "TX"],
                datasets: [

                    {
                        label: "2014",
                        fillColor: "rgba(128,0,128,0)",
                        strokeColor: "rgba(128,0,128,1)",
                        pointColor: "rgba(128,0,128,1)",
                        pointStrokeColor: "#fff",
                        pointHighlightFill: "#fff",
                        pointHighlightStroke: "rgba(128,0,128,1)",
                        data: [16, 17, 15, 15, 18, 16]
                    },
                    {
                        label: "2013",
                        fillColor: "rgba(80, 219, 102,0)",
                        strokeColor: "rgba(80, 219, 102, 1)",
                        pointColor: "rgba(80, 219, 102, 1)",
                        pointStrokeColor: "#fff",
                        pointHighlightFill: "#fff",
                        pointHighlightStroke: "rgba(80, 219, 102, 1)",
                        data: [10, 12, 10, 11, 7, 9]
                    }
                ]
            };

            });

修改

var name等于data_chinadata_china global var data.js我需要名称来调用它。

我的数据看起来像这样,我需要根据页面调用特定数据,每个页面都有自己的data_(名称)。但我需要一种方法来选择特定于页面的数据集。

如果我转到页面site.html?name=china我需要加载中国的site.html?name=uk数据,那么我需要它加载英国的data_uk并应用全球数据var到局部变量。有一百个不同的站点名称,所以我需要一种自动化的方法。

您通过创建var data = "";变量来调用charrt.js的数据集,因此我需要var data = data_china; (或data_(国家/地区的名称)),具体取决于页面。

Data.js

data_china = {
                labels: ["FR", "Fl", "FC", "P", "TR", "TX"],
                datasets:
                [

                    {
                        label: "2014",
                        fillColor: "rgba(128,128,128,0)",
                        strokeColor: "rgba(128,128,128,1)",
                        pointColor: "rgba(128,128,128,1)",
                        pointStrokeColor: "#fff",
                        pointHighlightFill: "#fff",
                        pointHighlightStroke: "rgba(128,0,128,1)",
                        data: [16, 17, 15, 15, 18, 16]
                    },
                    {
                        label: "2013",
                        fillColor: "rgba(102, 219, 102,0)",
                        strokeColor: "rgba(102, 219, 102, 1)",
                        pointColor: "rgba(102, 219, 102, 1)",
                        pointStrokeColor: "#fff",
                        pointHighlightFill: "#fff",
                        pointHighlightStroke: "rgba(80, 219, 102, 1)",
                        data: [10, 12, 10, 11, 7, 9]
                    }
                ]
            };
data_uk = {
                labels: ["FR", "Fl", "FC", "P", "TR", "TX"],
                datasets:
                [

                    {
                        label: "2014",
                        fillColor: "rgba(128,128,128,0)",
                        strokeColor: "rgba(128,128,128,1)",
                        pointColor: "rgba(128,128,128,1)",
                        pointStrokeColor: "#fff",
                        pointHighlightFill: "#fff",
                        pointHighlightStroke: "rgba(128,0,128,1)",
                        data: [16, 17, 15, 15, 18, 16]
                    },
                    {
                        label: "2013",
                        fillColor: "rgba(102, 219, 102,0)",
                        strokeColor: "rgba(102, 219, 102, 1)",
                        pointColor: "rgba(102, 219, 102, 1)",
                        pointStrokeColor: "#fff",
                        pointHighlightFill: "#fff",
                        pointHighlightStroke: "rgba(80, 219, 102, 1)",
                        data: [10, 12, 10, 11, 7, 9]
                    }
                ]
            };

1 个答案:

答案 0 :(得分:2)

如果我做对了,你需要的是在window对象上使用Bracket notation来调用它,例如: window[name]