javascript - 我可以在 html 中的一个脚本中有 2 个函数吗?
问题描述
我的页面有一个显示不同数据的多个“选项卡”。
这是我的标签功能。
function openEvt(evt, evtName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(evtName).style.display = "block";
evt.currentTarget.className += " active";
}
当用户单击“详细信息”选项卡时,我的页面将显示条形图。这是我的图表代码。我用函数 openEvt() 把它放在脚本中。
function setChart(){
var ctx = document.getElementById("detail");
var detail= new Chart(ctx, {
type: 'bar',
data: {
labels: {{ chartdata_labels|safe }},
datasets: [{
label: '# of votes',
data: {{ chartdata_data }},
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
}
window.onload = setChart;
这是我在数据表上方显示图表的代码。
<canvas id="detail" width="1200" height="400"></canvas>
当只有一个函数-openEvt() 时,它可以正常工作。但是当我在 setChart() 中添加时,该选项卡将不起作用并且没有任何数据显示。
解决方案
function SayHello() {
alert('Hello')
}
function SayWorld() {
alert('World')
}
function addWindowOnloadEvent(func) {
var oldWindowOnload = window.onload;
if (typeof func === 'function') {
if (typeof window.onload === 'function') {
window.onload = function () {
oldWindowOnload();
func();
}
} else {
window.onload = func;
}
}
}
addWindowOnloadEvent(SayHello);
addWindowOnloadEvent(SayWorld);
推荐阅读
- php - 在浏览器选项卡中从 php 脚本打开本地文件
- angular - 长时间延迟后的角度 setInterval 接收值
- javascript - 如何将变量从弹出窗口传递到内容脚本?
- python - FreeRADIUS Python 模块“属性 Auth-Type 的未知或无效值”Python“”
- azure-web-app-service - 在所有 Azure 应用服务实例上调用 WebRequest
- batch-file - 带有 Exec 批处理和文件 Stream 进度的 Inno Setup 安装程序页面永远不会退出 Stream 并且即使在正确的 bat 终止的情况下也无限期挂起
- php - 如何在没有 pjax 的情况下使用分页而不重新加载主页
- django - 如何在 django 中对索引 db 进行分组
- javascript - 在 Node 中,Buffer.length 是静态属性吗?
- python - 与 xarray.Dataset.mean 冲突的值?