代码之家  ›  专栏  ›  技术社区  ›  Adam

JS | Onload和Onclick不能一起工作-快速解决方案?

  •  0
  • Adam  · 技术社区  · 7 年前

    问题可能在于两个事件: 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>
    1 回复  |  直到 7 年前
        1
  •  0
  •   Sidney    7 年前

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