[英]onclick isn't working with other JS file
当onclick与Kill_Count不在同一个html文件中时,该onclick起作用了,但是当它们在一起时,只有Kill_Count文件执行,该按钮仍然显示,但是当您单击它时,什么也没有发生...
Button.html:
<html>
<head>
<script src="window.js"></script>
<script src="Kill_Count.js"></script>
</head>
<body>
<button type="button" id="Vegan Site">Vegan Site</button>
<table>
<tr>
<td width="424.2">
Some stuff about Animals.
</td>
<td>
<table cellpadding="0" cellspacing="0">
<tr>
<td>Chickens</td>
<td align="right">
<span id="Kill_Count"></span>
</td>
</tr>
<tr>
<td>Pigs</td>
<td align="right">
<span id="Kill_Count2"></span>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
window.js:
window.onload = function () {
function myFunction() {
window.open("http://www.abolitionistapproach.com/");
}
var Process = document.getElementById('Vegan Site');
Process.onclick = myFunction;
}
Kill_Count.js
function Kill_Count(id)
{
var animal = "Chickens";
var totalDeaths = 49877536490;
var deathsPerSecond = totalDeaths/365/24/60/60/4;
var deaths = 0;
var timer = 1;
setInterval(function() {
deaths = deathsPerSecond*timer;
result = deaths.toFixed();
document.getElementById(id).innerHTML = result;
timer++;
}, 250);
}
function Kill_Count2(id)
{
var animal = "Pigs";
var totalDeaths = 1375940758;
var deathsPerSecond = totalDeaths/365/24/60/60/4;
var deaths = 0;
var timer = 1;
setInterval(function() {
deaths = deathsPerSecond*timer;
result = deaths.toFixed();
document.getElementById(id).innerHTML = result;
timer++;
}, 250);
}
window.onload = Kill_Count('Kill_Count');
window.onload = Kill_Count2('Kill_Count2');
在最后一行中,您将覆盖前一行。 window.onload只能有一个值,当您分配两次时,最后一个获胜。 出于相同的原因,当您同时包含两个文件时,您将覆盖window.onload,并且window.js的window.js分配将被忽略。 请注意,由于您没有在Kill_Count中返回函数,因此实际上将undefined分配给window.onload,并且该函数在分配时就正确运行,而不是真正在加载时运行。 基本上,负载不会发生任何事情。
为了将多个侦听器添加到window.onload
,需要使用window.addEventListener("load", someFunction, false)
(和window.attachEvent("onload", someFunction);
如果您支持Internet Explorer 8及以下版本) 。 您可以多次调用它,并且所有功能都会在加载时触发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.