html - Bootstrap 模态框上的关闭交叉不起作用

标签 html css twitter-bootstrap

我正在使用 Bootstrap 3。我有一个模态框,我在其中使用以下 Bootstrap 代码在右上角插入了一个十字:

<button type="button" class="close" aria-label="Close">
    <span aria-hidden="true">&times;</span>
</button>

我实际上认为 Bootstrap class="close" 会关闭模式。但似乎我需要更多的东西才能让它发挥作用?

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


</head>
<body>
<div class="mk row">
    <a href="#">
        <div class="col-sm-12 margin_bottom">
            <div class="hover11 column">
                <figure>
                    <a href="#" data-toggle="modal" data-target="#nyhedsbrev-tilmelding">
                      <img src="https://dohanews.co/wp-content/uploads/2016/07/headline.png" alt="Sign up for our newsletter" class="img-responsive"></img>
                    </a>
                </figure>
            </div>
            <div class="modal fade" id="nyhedsbrev-tilmelding" role="dialog">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-body">
                            <div id="mc_embed_signup">
                                <button type="button" class="close" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                  <form action="https://zalis.us17.list-manage.com/subscribe/post?u=3be651219436ee0f217f040a5&amp;id=d07c036d2e" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>
                                    <div id="mc_embed_signup_scroll">
                              	     <h2>Subscribe to our mailing list</h2>
                                      <div class="indicates-required"><span class="asterisk">*</span> indicates required</div>
                                      <div class="mc-field-group">
                              	         <label for="mce-EMAIL">Email Address  <span class="asterisk">*</span></label>
                            	           <input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL">
                                      </div>
                                      <div class="mc-field-group">
                                      	<label for="mce-FNAME">First Name </label>
                                      	<input type="text" value="" name="FNAME" class="" id="mce-FNAME">
                                      </div>
                              	       <div id="mce-responses" class="clear">
                                    		<div class="response" id="mce-error-response" style="display:none"></div>
                                    		<div class="response" id="mce-success-response" style="display:none"></div>
                                    	</div> 
                                      <div style="position: absolute; left: -5000px;" aria-hidden="true"><input type="text" name="b_3be651219436ee0f217f040a5_d07c036d2e" tabindex="-1" value=""></div>
                                      <div class="clear">
                                          <input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button"></div>
                                  </div>
                              </form>
                            </div>
                        </div>
                        
                    </div>
                </div>
            </div>
            <div class="inner-wrapper bottom-left"></div>
        </div>
    </a>
</div>
<script>
 let $input = document.getElementById('mc-embedded-subscribe')
  $input.addEventListener('click', function (){
    this.value= "close";
    this.setAttribute('data-dismiss','modal');
  })
</script>
</body>
</html>

最佳答案

您需要将 data-dismiss="modal" 属性添加到按钮以关闭模式。

<button type="button" class="close" aria-label="Close" data-dismiss="modal">
    <span aria-hidden="true">&times;</span>
</button>

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


</head>
<body>
<div class="mk row">
    <a href="#">
        <div class="col-sm-12 margin_bottom">
            <div class="hover11 column">
                <figure>
                    <a href="#" data-toggle="modal" data-target="#nyhedsbrev-tilmelding">
                      <img src="https://dohanews.co/wp-content/uploads/2016/07/headline.png" alt="Sign up for our newsletter" class="img-responsive"/>
                    </a>
                </figure>
            </div>
            <div class="modal fade" id="nyhedsbrev-tilmelding" role="dialog">
                <div class="modal-dialog">
                    <div class="modal-content">
                      <div class="modal-header">
                      <button type="button" class="close" aria-label="Close" data-dismiss="modal"><span aria-hidden="true">&times;</span></button>
                      </div>
                        <div class="modal-body">
                            <div id="mc_embed_signup">
                                  <form action="https://zalis.us17.list-manage.com/subscribe/post?u=3be651219436ee0f217f040a5&amp;id=d07c036d2e" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>
                                    <div id="mc_embed_signup_scroll">
                              	     <h2>Subscribe to our mailing list</h2>
                                      <div class="indicates-required"><span class="asterisk">*</span> indicates required</div>
                                      <div class="mc-field-group">
                              	         <label for="mce-EMAIL">Email Address  <span class="asterisk">*</span></label>
                            	           <input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL">
                                      </div>
                                      <div class="mc-field-group">
                                      	<label for="mce-FNAME">First Name </label>
                                      	<input type="text" value="" name="FNAME" class="" id="mce-FNAME">
                                      </div>
                              	       <div id="mce-responses" class="clear">
                                    		<div class="response" id="mce-error-response" style="display:none"></div>
                                    		<div class="response" id="mce-success-response" style="display:none"></div>
                                    	</div> 
                                      <div style="position: absolute; left: -5000px;" aria-hidden="true"><input type="text" name="b_3be651219436ee0f217f040a5_d07c036d2e" tabindex="-1" value=""></div>
                                      <div class="clear">
                                          <input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button"></div>
                                  </div>
                              </form>
                            </div>
                        </div>
                        
                    </div>
                </div>
            </div>
            <div class="inner-wrapper bottom-left"></div>
        </div>
    </a>
</div>
<script>
 let $input = document.getElementById('mc-embedded-subscribe')
  $input.addEventListener('click', function (){
    this.value= "close";
    this.setAttribute('data-dismiss','modal');
  })
</script>
</body>
</html>

关于html - Bootstrap 模态框上的关闭交叉不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51836281/

相关文章:

css - 如何在字形图标内设置样式?

css - 图片未以全尺寸 Bootstrap 轮播显示

javascript - 如何将 iframe 高度和宽度设置为 100%

css - svg 无法在 ie9 中使用 css 调整大小

css - * { 盒子尺寸 : border-box; } : To border-box or not to border-box all elements?

jquery - 平稳地改变元素的位置

javascript - 仅当用户将其移动到底部时,如何在底部保持动态滚动?

html - 如何解决这种奇怪的表格行为?

asp.net - CSS如何改变文本的背景颜色?

angularjs - 在 Angular-ui Bootstrap 轮播之外获取事件幻灯片索引