【问题标题】:Modal Js to Jquery [closed]模态 Js 到 Jquery [关闭]
【发布时间】:2021-04-25 00:13:52
【问题描述】:

我有一些简单的模态。它在 js 中,但我需要将其移至 Jquery 并遇到问题。你能帮我么?如果需要,这里是Jsfiddle。如果高度设置为 100%,还有没有办法消除滚动?

const modal = document.getElementById("myModal");
const btn = document.getElementById("myBtn");
const span = document.getElementsByClassName("close")[0];
btn.onclick = function () {
  modal.style.display = "block";
};
span.onclick = function () {
  modal.style.display = "none";
};
window.onclick = function (event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
};
.modal {
  display: none;
  position: fixed;
  z-index: 9999;
  padding-top: 100px;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
  background-color: rgb(0, 0, 0);
  background-color: rgba(0, 0, 0, 0.4);
}

.modal-content {
  background-color: blue;
  margin: auto;
  width: 100%;
  height: 100%;
  padding-top: 2%;
}

.close {
  color: #aaaaaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
} 
 <div id="myModal" class="modal">     
      <div class="modal-content">
        <span class="close">&times;</span>
        <p>Some text </p>
      </div>    
    </div>
            <button  class = "" id="myBtn">Full ingridients</button>

【问题讨论】:

  • 欢迎来到 Stack Overflow!请参阅Why is “Can someone help me?” not an actual question? 如果您询问如何使用 jQuery 或任何特定的 jQuery 插件,那么您最好从一些介绍性教程开始。我们鼓励您进行尝试。如果在此尝试期间您遇到特定问题,例如错误或意外结果,我们可以提供帮助。要了解有关此社区的更多信息以及我们如何为您提供帮助,请从 tour 开始并阅读 How to Ask 及其链接资源。

标签: javascript jquery


【解决方案1】:

您可以将 .on() 函数与 ID 和 Class 选择器一起使用。请参考移除滚动bootstrap modal removes scroll bar

 $(document).ready(function () {
    $('#myBtn').on('click', function () {
        $('#myModal').modal('show');
        $('#myModal').css('display', 'block');
    });

    $('.close').on('click', function () {
        $('#myModal').modal('hide');
    });
});

【讨论】:

    猜你喜欢
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-09
    • 2019-10-07
    • 2016-12-03
    • 2011-08-01
    相关资源
    最近更新 更多