无法在页面加载时触发按钮或锚元素的点击事件

时间:2019-06-16 08:47:46

标签: javascript jquery html popup

<a id="mybutton" class="btn open-mod" href="#open-modal">? Basic CSS-Only Modal</a>

<div id="open-modal" class="modal-window">
  <div>
    <a href="#" title="Close" class="modal-close">Close</a>
    <h1>Voilà!</h1>
    <div>A CSS-only modal based on the :target pseudo-class. Hope you find it helpful.</div>
    <div><small>Sponsor</small></div>
    <a href="https://aminoeditor.com" target="_blank">? Amino: Live CSS Editor for Chrome</div>
    </div>
</div>

我有一个基于CSS的简单模式,可以在页面加载时显示该模式,而不是在用户单击链接上的按钮时显示。

我尝试了几件事,但是它不起作用,我使用链接以及按钮来触发事件,但是它不起作用

$(window).load(function(){
   // $(".open-mod").click();
  $("#mybutton")[0].click()
});

$(document).ready(function(){
  // $("#mybutton").trigger( "click" );
   //$("#mybutton").click();
});

不确定我在哪里做错了...

https://codepen.io/anon/pen/ewZYwy

2 个答案:

答案 0 :(得分:2)

您尚未为按钮编写点击功能。

$(document).ready(function(){
   $("#mybutton").click();
});


$("#mybutton").on('click', function() {
  $($(this).attr('href')).fadeToggle();
})
.modal-window {
  position: fixed;
  background-color: rgba(255, 255, 255, 0.25);
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 999;
  display: none;
  pointer-events: none;
  -webkit-transition: all 0.3s;
  -moz-transition: all 0.3s;
  transition: all 0.3s;
}
.modal-window:target {
  opacity: 1;
  pointer-events: auto;
}
.modal-window > div {
  width: 400px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 2em;
  background: #ffffff;
}
.modal-window header {
  font-weight: bold;
}
.modal-window h1 {
  font-size: 150%;
  margin: 0 0 15px;
}

.modal-close {
  color: #aaa;
  line-height: 50px;
  font-size: 80%;
  position: absolute;
  right: 0;
  text-align: center;
  top: 0;
  width: 70px;
  text-decoration: none;
}
.modal-close:hover {
  color: black;
}

/* Demo Styles */
html,
body {
  height: 100%;
}

body {
  font: 600 18px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
  background-image: linear-gradient(to right, #7f53ac 0, #657ced 100%);
  color: black;
}

a {
  color: inherit;
}

.container {
  display: grid;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.modal-window div:not(:last-of-type) {
  margin-bottom: 15px;
}

small {
  color: #aaa;
}

.btn {
  background-color: #fff;
  padding: 1em 1.5em;
  border-radius: 3px;
  text-decoration: none;
}
.btn i {
  padding-right: 0.3em;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a id="mybutton" class="btn open-mod" href="#open-modal">? Basic CSS-Only Modal</a>

<div id="open-modal" class="modal-window">
  <div>
    <a href="#" title="Close" class="modal-close">Close</a>
    <h1>Voilà!</h1>
    <div>A CSS-only modal based on the :target pseudo-class. Hope you find it helpful.</div>
    <div><small>Sponsor</small></div>
    <a href="https://aminoeditor.com" target="_blank">? Amino: Live CSS Editor for Chrome</div>
    </div>
</div>

答案 1 :(得分:0)

只需使用

$(document).ready(function(){
$('#idofmodal').modal('show')
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>