在JavaScript中调用函数

时间:2012-12-19 11:24:48

标签: javascript jquery html url inject

由于应将url JavaScript注入到webview中,有人可以告诉我如何在以下代码中调用addPoints()函数内的basic函数吗?

<html>
<head>
<style type="text/css">
    #example {
        width: 650px;
        height: 450px;
    }
</style>
</head>
 <body>
      <div id="example"></div>
      <div id="controls">
           <button>Blue</button>
           <button>Green</button>
           <button>Red</button>
    </div>
     <script type="text/javascript" src="js/chart-high/jquery-1.8.3.js">                 </script>  
     <script type="text/javascript" src="js/chart-flotr2/flotr2.min.js"></script>
     <script type="text/javascript">

        (function basic(container) {

            var
               _ = Flotr._,
                d1 = [[0, 3], [4, 8], [8, -8], [9, 16]],
                d2 = [],
                d3 = [],
                show = [true, true, true],
                i,
                graph;

            // Generate first data set
            for (i = 0; i < 14; i += 0.5) {
                d2.push([i, Math.sin(i)]);
            }

            for (i = 0; i < 14; i += 1) {
                var randomN3 = Math.floor(Math.random() * 201) - 100
                d3.push([i, randomN3]);
            }

            $('#controls').delegate('button', 'click', function(e) {
                var
                index = $(e.currentTarget).index();
                show[index] = !show[index];
                drawGraph();
            });

            function **addPoints(dd1, dd2, dd3)** {
                var
                data = [
                    {data: dd1},
                    {data: dd2},
                    {data: dd3}
                ];

                _.each(show, function(show, index) {
                    data[index].hide = !show;
                });

                // Draw Graph
                return Flotr.draw(container, data, {
                    xaxis: {
                        minorTickFreq: 4
                    },
                    grid: {
                        minorVerticalLines: true
                    }
                }); 
            }

            function drawGraph() {
                var
                data = [
                    {data: d1},
                    {data: d2},
                    {data: d3}
                ];

                _.each(show, function(show, index) {
                    data[index].hide = !show;
                });

                // Draw Graph
                return Flotr.draw(container, data, {
                    xaxis: {
                        minorTickFreq: 4
                    },
                    grid: {
                        minorVerticalLines: true
                    }
                });
            }

            drawGraph();

        })(document.getElementById("example"));
    </script>
</body>

以及如何在以下代码中调用addPaoints函数?

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="js/chart-high/jquery-1.8.3.js"></script>
<script type="text/javascript" src="js/chart-high/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(function() {
    var chart;
    $(document).ready(function() {

        chart = new Highcharts.Chart({

            chart : {
                renderTo : 'container',
                type : 'line'
            },
            title : {
                text : 'Trycksignaler',
                x : -20
            //center
            },
            yAxis : {
                title : {
                    text : 'CAN-värden'
                }
            },
            series : [ {
                name : 'B4',
                data : [ 7.0, 6.9 ]
            }, {
                name : 'B5',
                data : [ -0.2, 0.8 ]
            }, {
                name : 'Overload',
                data : [ -0.9, 0.6 ]
            } ]

        });
    });

    function **addPaoints**(valY0, valY1, valY2) {
        for (i = 0; i < valY0.length; i++) {
            chart.series[0].addPoint(valY0[i]);
            chart.series[1].addPoint(valY1[i]);
            chart.series[2].addPoint(valY2[i]);
        }
    }

});
</script>
<script src="js/chart-high/highstock.js"></script>
<script src="js/chart-high/modules/exporting.js"></script>
</head>
<body bgcolor="#030303">
<div id="container" style="height: 400px; width: 700px"></div>
<!--        <button id="button" >Add point</button> -->
​
</body>
</html>

1 个答案:

答案 0 :(得分:3)

addPoints在函数内定义(因此限定为该函数)。

没有修改它就无法从它定义的函数外部调用它。

这通常是通过让var basic_namespace = (function basic(container) {并让该函数返回包含公共方法的对象来实现的:

return { "addPoints": addPoints };

然后您可以致电:

basic_namespace.addPoints()

另见the module pattern