gpt4 book ai didi

javascript - 在 JavaScript 中调用函数

转载 作者:行者123 更新时间:2023-11-28 20:43:17 24 4
gpt4 key购买 nike

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

<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>

最佳答案

addPoints 在函数内部定义(因此作用域为该函数)。

如果不修改它,就无法从定义它的函数外部调用它。

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

return { "addPoints": addPoints };

此时您可以调用:

basic_namespace.addPoints()

另请参阅the module pattern .

关于javascript - 在 JavaScript 中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13951332/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com