admin管理员组

文章数量:1323510

I've got a function changeGraph() inside the jQuery-wrapper which I need to call somehow from outside it. I need to access the function setData from the jQuery based graph-library Flot.

The source is looking like this:

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 }
          }
       }
    );


});

How can I acplish this?

I've got a function changeGraph() inside the jQuery-wrapper which I need to call somehow from outside it. I need to access the function setData from the jQuery based graph-library Flot.

The source is looking like this:

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 }
          }
       }
    );


});

How can I acplish this?

Share edited Jun 26, 2011 at 20:37 Hedge asked Jun 26, 2011 at 20:25 HedgeHedge 16.8k45 gold badges154 silver badges260 bronze badges 6
  • Can you move the function outside of the function scope? – Jared Farrish Commented Jun 26, 2011 at 20:27
  • I need to access this variable inside the scope $.plot($("#placeholder") , I don't know to do this outside the scope – Hedge Commented Jun 26, 2011 at 20:29
  • @Hedge - Can you post more of your code? I don't know why you couldn't have the changeGraph() in the global scope. You might consider, if you need to, using jQuery's $.data() to pass data into your function. – Jared Farrish Commented Jun 26, 2011 at 20:31
  • You can still use $ if you move your function outside, like I showed in my answer. – jtbandes Commented Jun 26, 2011 at 20:31
  • @jtbandes - I suspect he's got other functions in that scope. The OP needs to provide more context for his problem by posting more code, I think. – Jared Farrish Commented Jun 26, 2011 at 20:34
 |  Show 1 more ment

3 Answers 3

Reset to default 8
var changeGraph;

$(function () {

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

});

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

You should move your function outside of the callback function.

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

本文标签: javascriptHow to call inner function of jQuerywrapper (function()Stack Overflow