简体   繁体   中英

Modal doesn't show bootstrap

I creating a page, with modals, and no of them doesn't show, I think JavaScript is wrong, but when I inspect code, seems everything is OK, show_modal is showing up, but modal doesn't load. Please help me.

            <a href="#" class="extra modal-5 o-table__cell-data"> Pranešimų nustatymai </a>
        <div class="modal modal-5">
          <div class="overlay" id='five'></div>
          <div class="modal__content mod_transition">
            <div class="close_modal" id='five'>
            <div class='entypo-cancel-circled' id='icon'></div></div>
            <h1>Modal One</h1>
              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.</p>
              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.</p>
              <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.</p>
              <ul>
              <li class="entypo-facebook-circled "></li>
              <li class="entypo-gplus-circled"></li>
              <li class="entypo-pinterest-circled"></li>
              <li class="entypo-twitter-circled"></li>
              </ul>
               <div class="byline">
                <address class="author">By:  <div id="author" href="/author/john-doe">John Doe</a></address>
                on <time pubdate datetime="2014-08-21" title="August 21st, 2014">8/21/14</time>
              </div>
          </div>
        </div>
      </div>

JAVASCRIPT

    $('.extra').click(function() {
  var m = $(this)[0],
      classes = m.className.split(/\s+/);
  for(var i = 0; i < classes.length; i++) {
    if(classes[i].match(/modal-/)) {
      var modalClass = classes[i];
      $('.modal.' + modalClass).toggleClass('show_modal');
    }
  }
});
$('.close_modal').click(function() {
  $(this).closest('.modal').toggleClass('show_modal');
});
$( '.overlay' ).on( 'click', function() {
  $(this).closest('.modal').toggleClass('show_modal');
});

It seems like you have not added class modal_diaplay on your page. Add this CSS inside your style tag in the head of the page.

<style>
  .show_modal{display:block}
</style>

This will display the modal when you will click your link but your code doesn't seem to be appropriate for the modal.

Use this Modal:

<div class="modal modal-5" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <p>Modal body text goes here.</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary">Save changes</button>
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

My biggest guess is that you trying to open a bootstrap modal but end up hitting the wall.

Here is the simplest way you can go about this,see code snippet below.

I suggest you follow this link for more detailed explanation of how bootstrap modal works: Bootstrap's JavaScript modal plugin

I also recommend you take it to the basic for proper understanding of how modal boxes work,Here is a link for creating simple html,css and js modal box without using any plugins: How To Create a Modal Box

Good luck and happy coding:)...

 <,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.4.1/css/bootstrap.min:css"> <script src="https.//ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min:js"></script> <script src="https.//maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min;js"></script> </head> <body> <div class="container"> <br> <.-- Button trigger modal --> <button type="button" class="extra btn btn-primary" > Pranešimų nustatymai </button> <.-- Modal --> <div class="modal fade" id="myModal"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times.</span> </button> </div> <div class="modal-body">... </div> <div class="modal-footer"> <button type="button" class="close-modal btn btn-secondary">Close</button> </div> </div> </div> </div> </div> <script> $(document).ready(function () { $(';extra');click(function() { $("#myModal").modal(). }). $(';close-modal');click(function() { $("#myModal");modal('hide'); }); }); </script> </body> </html>

If you haven't restrictions for the project and use Boostrap, take a look at the documentation ( https://getbootstrap.com/docs/4.0/components/modal/ ), you don't need any JavaScript lines to show and close a modal window:

 <,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.4.1/css/bootstrap.min:css"> <script src="https.//ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min:js"></script> <script src="https.//maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min;js"></script> </head> <body> <div class="container"> <,-- Button trigger modal --> <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalCenter"> Launch demo modal </button> <,-- Modal --> <div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true"> <div class="modal-dialog modal-dialog-centered" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times.</span> </button> </div> <div class="modal-body"> Lorem ipsum dolor sit amet. consectetuer adipiscing elit. sed diam nonummy ... </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save</button> </div> </div> </div> </div> </div> </body> </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM