JS | Onload 与 Onclick 不能协同工作 - 快速解决方案?

JS | Onload with Onclick does not work together - quick solution?

问题可能出在两个事件上:onloadonclick(事件onload有效,但onclick无效)。我怎样才能以最好的方式解决这个问题,让它们中的两个 运行 保持在同一代码中?感谢您的任何建议。

const vacationPlaces = ['Praha','Vien','Munich'];

function backwards() {
for (let vacationSpotIndex = vacationPlaces.length - 1; vacationSpotIndex >= 0; vacationSpotIndex--) {
    let showPlaces = vacationPlaces[vacationSpotIndex] + ", ";
    let removeComma = showPlaces;
    document.getElementById("resultMonthly").innerHTML += removeComma;
 }
}

function forwards() {
for (let i = 0; i < vacationPlaces.length; i++) {
    document.getElementById("resultDaily").innerHTML += vacationPlaces[i] + ", ";
 }
}



function all() {
    backwards();
    forwards();
}

function click() {
    document.getElementById("resultHourly").innerHTML = "hi";
}
<!DOCTYPE html>
<html>
    <head>
        
        <meta charset="utf-8">
        <title>Issue App</title>
        <link rel="stylesheet" type="text/css" href="wageup.css">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
        
    </head>
    <body onload="all()">
    <div class="container">
        <h2>for loop</h2>
        
        <div class="alert alert-success" role="alert" id="resultMonthly">
        </div>
        <div class="alert alert-info" role="alert" id="resultDaily">
        </div>
        <div onclick="click()" class="alert alert-warning" role="alert" id="resultHourly">
        </div>
        </div>

        
        
                
        <!-- Scripts -->
        
        <script src="http://chancejs.com/chance.min.js"></script>
        <!-- Jquery -->
        <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
        <!-- Bootstrap -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
        <!-- JS -->
        <script type="text/javascript" src="forloop.js"></script>
        
    </body>
</html>

onclick 属性的上下文中,函数 click 是本机函数。所以您的点击处理程序正在运行,但它调用的是内置 click 函数,而不是您的点击函数。您可以通过重命名函数来解决这个问题。比如我命名为showAlert:

const vacationPlaces = ['Praha','Vien','Munich'];

function backwards() {
  for (let vacationSpotIndex = vacationPlaces.length - 1; vacationSpotIndex >= 0; vacationSpotIndex--) {
    let showPlaces = vacationPlaces[vacationSpotIndex] + ", ";
    let removeComma = showPlaces;
    document.getElementById("resultMonthly").innerHTML += removeComma;
  }
}

function forwards() {
  for (let i = 0; i < vacationPlaces.length; i++) {
    document.getElementById("resultDaily").innerHTML += vacationPlaces[i] + ", ";
  }
}

function all() {
  backwards();
  forwards();
}

function showAlert() {
  console.log('clicked')
  document.getElementById("resultHourly").innerHTML = "hi";
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Issue App</title>
  <link rel="stylesheet" type="text/css" href="wageup.css">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"crossorigin="anonymous">

  <!-- Scripts -->
  <script src="http://chancejs.com/chance.min.js"></script>
  <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <!-- <script type="text/javascript" src="forloop.js"></script> -->
</head>

<body onload="all()">
  <div class="container">
    <h2>for loop</h2>

    <div class="alert alert-success" role="alert" id="resultMonthly"></div>
    <div class="alert alert-info" role="alert" id="resultDaily"></div>
    <div onclick="showAlert()" class="alert alert-warning" role="alert" id="resultHourly"></div>
  </div>
</body>
</html>