dqrm8199 2019-05-05 10:34
浏览 398

如何通过id获取span元素?

Actually i create a popup with close icon and i try to close popup form using span method. I want to get span element value by element id.

       <script>
  // Get the modal
  var modal = document.getElementById('myModal');

  // Get the button that opens the modal
  var btn = document.getElementById("myBtn");

  // Get the <span> element that closes the modal
  var span = document.getElementsByClassName("close")[1];

  // When the user clicks the button, open the modal
  btn.onclick = function() {
    modal.style.display = "block";
  }

  // When the user clicks on <span> (x), close the modal
  span.onclick = function() {
    modal.style.display = "none";
  }

  // When the user clicks anywhere outside of the modal, close it
  window.onclick = function(event) {
    if (event.target == modal) {
      modal.style.display = "none";
    }
  }
  </script>
  • 写回答

2条回答 默认 最新

  • dongque8332 2019-05-05 11:19
    关注

    You can try to use getElementByTagName or getElementById instead of by the class.

    Alternatively, try to add the attribute onclick directly in the DOM on the wanted span element, and then call a function which does what you want.

    Hope it helped.

    评论

报告相同问题?