简体   繁体   English

模态在Bootstrap中不起作用

[英]The modal is not working in Bootstrap

I'm building a website by using Bootstrap and when i tried to use modal it didn't work me! 我正在使用Bootstrap构建一个网站,当我尝试使用模态时它不起作用! I mean it didn't pop-up. 我的意思是它没有弹出。 I've searched every single solution but couldn't solve it. 我搜索过每一个解决方案,但无法解决。 Then I found 然后我找到了

Best Regards 最好的祝福

<div class="modal fade" id="myModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type"button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="myModalLabel"><i class="fa fa-envelope"></i>Subscribe to our Mailing List</h4>
            </div><!--modal header-->
            <div class="modal-body">
                <p>Simply enter your name and email! As a thank you for joining us, we're going to give you one of our best-selling courses, <em>for free!</em></p>
                <form class="form-inline" role="form">
                    <div class="form-group">
                        <label class="sr-only" for="subscribe-name">Your first name</label>
                        <input type="text" class="form-control" id="subscribe-name" placeholder="Your first name">
                    </div><!--form group-->
                </form><!--form-->
                <form class="form-inline" role="form">
                    <div class="form-group">
                        <label class="sr-only" for="subscribe-email">and your email</label>
                        <input type="text" class="form-control" id="subscribe-email" placeholder="and your email">
                    </div><!--form group-->
                    <input type="submit" class="btn btn-danger" value="Subscribe!">
                </form><!--form-->
                <hr>
                <p><small>By providing your email you consent to receiving occosional promotional emails & newsletter. <br>No Spam. Just good stuff. We respect your privacy & you may unsubscribe at any time.</small></p>
            </div><!--modal-body-->
        </div><!--modal-content-->
    </div><!--modal-dialog-->
 </div><!--modal-->



<!--BOOTSTRAP CORE JAVASCRIPT
   ==========================-->
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
   <script src="assets/js/jquery-2.2.4.min.js"></script>
   <script src="assets/js/bootstrap.min.js"></script>
   <script src="assets/js/main.js"></script>

Then I found this code and added my "main.js" file then it worked fine but after this when I use my webpage on phone and click on the right top it opened modal instead of opening navbar. 然后我找到了这个代码,并添加了我的“main.js”文件,然后它工作正常,但在此之后,当我在手机上使用我的网页并点击右上方时,它打开模态而不是打开导航栏。 Here is the image! 这是图像!

You can find what I meant here! 你可以在这里找到我的意思!

PS: Sorry if i make any misspelling! PS:对不起,如果我拼错了!

$('button').click(function(){
 $('#myModal').modal('show');
});

You should always look for 'specific' element instead of using 'common' elements in your DOM. 你应该总是寻找'特定'元素而不是在DOM中使用'common'元素。

You current code fail as you have bound a click event on 'button' element which means on any 'button' element of your DOM you want to show up a modal. 您当前的代码失败,因为您在“按钮”元素上绑定了一个单击事件,这意味着您想要显示模式的DOM的任何“按钮”元素。

What you should do is following, 你应该做的是跟随,

// this will add click event on only button with specific id i.e. button_id
$('button#button_id').click(function(){
   $('#myModal').modal('show');
});

or 要么

// this will add click event on only button with specific class i.e. button_class
$('button.button_class').click(function(){
   $('#myModal').modal('show');
});

Also main thing to look here is why your modal is not opened without this js snippet you added. 此外,主要的一点是,如果没有你添加的这个js片段,你的模态就不会被打开。

Don't declare it in global. 不要在全球范围内声明它。 ie $('button') . ie $('button') Instead, As @Bilal suggest, use the button with some class name or id. 相反,正如@Bilal建议的那样,使用带有某个类名或ID的按钮。

$('button.classname').click(function(){
  $('#myModal').modal('show');
});

Or 要么

$('button#id-name').click(function(){
  $('#myModal').modal('show');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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