如何调用jQuery-wrapper $(function(){}的内部函数?

时间:2011-06-26 20:25:29

标签: javascript jquery

我在jQuery-wrapper中有一个函数changeGraph(),我需要从外面以某种方式调用它。我需要从基于jQuery的图库Flot中访问函数setData。

来源看起来像这样:

function changeGraph(){
    // I need to access $.plot.setData somehow
};  

var d2 = [[0, 0], [20, 300000]];

$(function () {              
    $.plot($("#placeholder"), 
    [{color: "#000000", data: d2}],
    {

    series: {
        lines: { show: true, fill:true, fillColor: {colors: [ "#d1ddea","#8e959d"]}},
        points: { show: false }
          }
       }
    );


});

我该如何做到这一点?

3 个答案:

答案 0 :(得分:8)

var changeGraph;

$(function () {

    changeGraph = function () {
        // Need to access $.plot($("#placeholder") here
    };

});

changeGraph(); // call this when document is ready at least

答案 1 :(得分:2)

您应该将函数移出回调函数。

答案 2 :(得分:2)

function changeGraph() {
    // ...
}
$(function() {
    changeGraph();
});