Bootstrap 3模态窗口的示例代码不工作

时间:2015-04-24 12:14:03

标签: javascript jquery twitter-bootstrap twitter-bootstrap-3 bootstrap-modal

这个问题很简单,令人尴尬。作为对某些内容的测试,我将代码从http://getbootstrap.com/javascript/#modals直接复制到仅包含基本页面设置的页面,但它不起作用。这意味着我做了一些非常愚蠢的事情。这是我的整个页面:

<!DOCTYPE html>

<html>
    <head>

        <!-- Website Title & Description for Search Engine purposes -->
        <title></title>
        <meta name="description" content="">

        <!-- Mobile viewport optimized -->
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

        <!-- Bootstrap CSS -->
        <link href="includes/css/bootstrap.css" rel="stylesheet">
        <link href="includes/css/bootstrap-glyphicons.css" rel="stylesheet">

        <!-- Custom CSS -->
        <link href="includes/css/styles.css" rel="stylesheet">

        <!-- Include Modernizr in the head, before any other Javascript -->
        <script src="includes/js/modernizr-2.6.2.min.js"></script>

    </head>
    <body>

    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo">Open modal for @mdo</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@fat">Open modal for @fat</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@getbootstrap">Open modal for @getbootstrap</button>
...more buttons...

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="exampleModalLabel">New message</h4>
      </div>
      <div class="modal-body">
        <form>
          <div class="form-group">
            <label for="recipient-name" class="control-label">Recipient:</label>
            <input type="text" class="form-control" id="recipient-name">
          </div>
          <div class="form-group">
            <label for="message-text" class="control-label">Message:</label>
            <textarea class="form-control" id="message-text"></textarea>
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Send message</button>
      </div>
    </div>
  </div>
</div>


    <!-- All Javascript at the bottom of the page for faster page loading -->

    <!-- First try for the online version of jQuery-->
    <script src="http://code.jquery.com/jquery.js"></script>

    <!-- If no online access, fallback to our hardcoded version of jQuery -->
    <script>window.jQuery || document.write('<script src="includes/js/jquery-1.8.2.min.js"><\/script>')</script>

    <!-- Bootstrap JS -->
    <script src="includes/js/bootstrap.js"></script>

    <!-- Custom JS -->
    <script src="includes/js/script.js"></script>


<script type="text/javascript"> 
$('#exampleModal').on('show.bs.modal', function (event) {
  var button = $(event.relatedTarget) // Button that triggered the modal
  var recipient = button.data('whatever') // Extract info from data-* attributes
  // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
  // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
  var modal = $(this)
  modal.find('.modal-title').text('New message to ' + recipient)
  modal.find('.modal-body input').val(recipient)
})
</script>   


</body>
</html>

然而,当我点击按钮时,模式已启动,而不是&#34;新消息到@ mdo&#34;就像你进入他们的网站一样,我得到了未定义的新消息&#34;。我错过了什么?

3 个答案:

答案 0 :(得分:0)

     var recipient = button.data('whatever')

button.data('whatever')未定义

答案 1 :(得分:0)

首先,它并不尴尬,我们在那里。你的第二个代码看起来对我很好。唯一让我感到兴奋的是这条线:

&#13;
&#13;
<script src="includes/js/bootstrap.js"></script>
&#13;
&#13;
&#13;

这个目录存在吗?因为如果你从网站上复制它,它可能是指存储在服务器中的文件。尝试自己包含bootstrap.js。同样的事情为css文件

答案 2 :(得分:0)

哦,圣洁的牛。问题(显然)从来没有与代码。它是我加载的JQuery或Bootstrap的版本。当我用源链接替换我的本地副本时,问题就消失了。我很高兴我发现了这个问题,或者当我开发它时它会遍布我的整个网站。谢谢大家的帮助。我想你必须在成为专家之前先学习愚蠢的东西。