简体   繁体   English

onclick无法与其他JS文件一起使用

[英]onclick isn't working with other JS file

The onclick works when its not in the same html file as Kill_Count, but when they're together, only the Kill_Count file executes, the button still shows up, but when you click it, nothing happens... 当onclick与Kill_Count不在同一个html文件中时,该onclick起作用了,但是当它们在一起时,只有Kill_Count文件执行,该按钮仍然显示,但是当您单击它时,什么也没有发生...

Button.html: 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.js:

window.onload = function () {
    function myFunction() {
        window.open("http://www.abolitionistapproach.com/");
    }

    var Process = document.getElementById('Vegan Site');
    Process.onclick = myFunction;
}

Kill_Count.js 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');

In the last line, you override the previous line. 在最后一行中,您将覆盖前一行。 window.onload can only have one value and when you assign it twice, the last wins. window.onload只能有一个值,当您分配两次时,最后一个获胜。 For the same reason, when you include both of the files, you override window.onload and the window.js assignment to window.onload is ignored. 出于相同的原因,当您同时包含两个文件时,您将覆盖window.onload,并且window.js的window.js分配将被忽略。 Note that since you do not return a function in the Kill_Count, you actually assign undefined to window.onload and the function is run right when you assign it and not really on load. 请注意,由于您没有在Kill_Count中返回函数,因此实际上将undefined分配给window.onload,并且该函数在分配时就正确运行,而不是真正在加载时运行。 Basically, nothing happens on load. 基本上,负载不会发生任何事情。

In order to add multiple listeners to window.onload , you need to use window.addEventListener("load", someFunction, false) (and window.attachEvent("onload", someFunction); , if you support Internet Explorer 8 and below). 为了将多个侦听器添加到window.onload ,需要使用window.addEventListener("load", someFunction, false) (和window.attachEvent("onload", someFunction);如果您支持Internet Explorer 8及以下版本) 。 You can call it multiple times and all of the functions will fire on load. 您可以多次调用它,并且所有功能都会在加载时触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM