这些 javascript 函数以什么顺序运行?

我有一个将变量加 1 的按钮,但我想在用户单击它后隐藏该按钮以阻止他们添加多个。该按钮要么不隐藏,要么不添加。请帮忙。我尝试了一些东西,但没有任何效果,它是同样的问题,一个或另一个都有效,但从来没有。


<!DOCTYPE html>

<html>

<head>

    <script type="text/javascript"></script>

    <script>   

  var num = 1;

  window.addEventListener("load", function() { // on page loade

    document.getElementById("join").addEventListener("click", function(event) {

      num++;

      show()

    })

    show(); // first time

  })

  const pad = (num, howMany, what) => (Array(howMany).join(what) + num).slice(-howMany);


  function show() {

    document.getElementById('followers').innerHTML = pad(num, 10, "0")

  }


  </script>


  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

  <script>

    $(document).ready(function(){

    $(".btn1").click(function(){

      $(this).hide();

    });

  });

  </script>



</head>

<body>

  <div class="counter-wrap">

  <div id="followers" class="counter">0,000</div>

  <div class="measure-wrap">

    <span class="text-uppercase letter-spacing" style="top: 4px;">People</span>

  </div>

</div>

<div id="idid">

TEst

<p>This is a paragraph.</p>

</div>

<button id="btn" class="button button2 btn1">Hide</button>

</body>

<style type="text/css">


  .button {

      opacity: 1;

      display: block;

      margin-left: auto;

      margin-right: auto;

      width: 25%;

      border-radius: 30px;

        text-align: center;

        text-decoration: none;

        background-color: #1D1D1D;

        border: none;

        color: white;

        padding: 15px 42px;

        font-size: 25px;

        cursor: pointer;

        outline: none;

    }

.button2 {

        box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24),0 17px 50px 0 rgba(0,0,0,0.19);

    }

.button:active {

        background-color: #080808;

      box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24),0 17px 50px 0 rgba(0,0,0,0.19);

      transform: translateY(4px);

    }



翻过高山走不出你
浏览 152回答 1
1回答

慕姐8265434

您没有选择页面上的元素。使用该querySelector方法,您可以选择与您提供的选择器匹配的第一个元素。$(document).ready(function() {&nbsp; $(".btn1").click(function() {&nbsp; &nbsp; $(this).hide();&nbsp; });});var num = 1;window.addEventListener("load", function() { // on page loade&nbsp; document.querySelector(".btn1").addEventListener("click", function(event) {&nbsp; &nbsp; num++;&nbsp; &nbsp; show()&nbsp; })&nbsp; show(); // first time})const pad = (num, howMany, what) => (Array(howMany).join(what) + num).slice(-howMany);function show() {&nbsp; document.getElementById('followers').innerHTML = pad(num, 10, "0")}.button {&nbsp; opacity: 1;&nbsp; display: block;&nbsp; margin-left: auto;&nbsp; margin-right: auto;&nbsp; width: 25%;&nbsp; border-radius: 30px;&nbsp; text-align: center;&nbsp; text-decoration: none;&nbsp; background-color: #1D1D1D;&nbsp; border: none;&nbsp; color: white;&nbsp; padding: 15px 42px;&nbsp; font-size: 25px;&nbsp; cursor: pointer;&nbsp; outline: none;}.button2 {&nbsp; box-shadow: 0 12px 16px 0 rgba(0, 0, 0, 0.24), 0 17px 50px 0 rgba(0, 0, 0, 0.19);}.button:active {&nbsp; background-color: #080808;&nbsp; box-shadow: 0 12px 16px 0 rgba(0, 0, 0, 0.24), 0 17px 50px 0 rgba(0, 0, 0, 0.19);&nbsp; transform: translateY(4px);}.counter-wrap {&nbsp; text-align: center;&nbsp; padding: .75rem 2rem 1.25rem;&nbsp; display: inline-block;&nbsp; margin: 0 auto;&nbsp; background: url(../images/counter-starburst-blue.svg);&nbsp; background-position: top center;&nbsp; background-repeat: no-repeat;&nbsp; background-size: 70px auto;&nbsp; width: 100%;}*,&nbsp;::after,&nbsp;::before {&nbsp; box-sizing: border-box;}.letter-spacing {&nbsp; letter-spacing: 2px;}.text-uppercase {&nbsp; text-transform: uppercase!important;&nbsp; font-family: Montserrat, sans-serif;&nbsp; color: #4d4f54;}.counter {&nbsp; display: inline-block;&nbsp; margin: 1.25rem .75rem .25rem .75rem;&nbsp; font-size: 2rem;&nbsp; line-height: .875;&nbsp; font-weight: 900;&nbsp; color: #273654;}<html><head>&nbsp; <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script></head><body>&nbsp; <div class="counter-wrap">&nbsp; &nbsp; <div id="followers" class="counter">0,000</div>&nbsp; &nbsp; <div class="measure-wrap">&nbsp; &nbsp; &nbsp; <span class="text-uppercase letter-spacing" style="top: 4px;">People</span>&nbsp; &nbsp; </div>&nbsp; </div>&nbsp; <div id="idid">&nbsp; &nbsp; TEst&nbsp; &nbsp; <p>This is a paragraph.</p>&nbsp; </div>&nbsp; <button id="btn" class="button button2 btn1">Hide</button></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript