14

我已经包含了来自 twitter-bootstrap 的示例代码,用于模态窗口。如果我点击按钮,它会打开模态窗口。但是,如果我尝试通过 javascript 显示模式窗口,则会收到以下错误 -

$("#myModal").modal("show")

TypeError: Object [object Object] has no method 'modal'

此错误仅出现在 chrome 中并在 Firefox 中有效。

下面是 HTML。“project-add-modal”是模态框的 id。导航到 Project -> Add 以查看模态按钮。-

<html class=" js flexbox canvas canvastext no-webgl no-touch geolocation postmessage websqldatabase indexeddb hashchange history draganddrop websockets rgba hsla multiplebgs backgroundsize borderimage borderradius boxshadow textshadow opacity cssanimations csscolumns cssgradients cssreflections csstransforms no-csstransforms3d csstransitions fontface generatedcontent video audio localstorage sessionstorage webworkers applicationcache svg inlinesvg smil svgclippaths" lang="en"><!--<![endif]--><head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

    <title>Project Management</title>
    <meta name="description" content="">
    <meta name="author" content="">

    <meta name="viewport" content="width=device-width">

    <link rel="stylesheet" href="css/style.css">
        <link rel="stylesheet" href="css/bootstrap.min.css">
        <link rel="stylesheet" href="css/bootstrap-responsive.min.css">

      <link rel="stylesheet" href="css/docs.css">

    <script src="//www.google-analytics.com/ga.js"></script><script src="js/libs/modernizr-2.5.3.min.js"></script>
</head>
<body class="">
<div id="header"><div id="top-navbar">
      <div class="navbar navbar-fixed-top">
        <div class="navbar-inner">
          <div class="container">
            <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </a>
            <div class="nav-collapse">
              <ul data-navlinks="left" class="nav">
                <li class="dropdown">
                  <a href="#" class="dropdown-toggle" data-toggle="dropdown">Projects<b class="caret"></b></a>
                  <ul class="dropdown-menu">
                    <li><a href="#" id="add-new-project" bind="click: pm.client.templating.hello">
                      Add
                    </a></li>
                  </ul>
                </li>
                <li class="dropdown">
                  <a href="#" class="dropdown-toggle" data-toggle="dropdown">Admin<b class="caret"></b></a>
                  <ul class="dropdown-menu">
                    <li><a href="#">Invite Users</a></li>
                  </ul>
                </li>
              </ul>
              <ul class="nav pull-right">
                <li class="dropdown">
                  <a href="#" class="dropdown-toggle" data-toggle="dropdown">Username<b class="caret"></b></a>
                  <ul class="dropdown-menu">
                    <li><a href="#">Logout</a></li>
                  </ul>
                </li>
              </ul>
            </div><!-- /.nav-collapse -->
          </div>
        </div><!-- /navbar-inner -->
      </div>
    </div></div>
<div class="container cljs-main"><div id="project-add-section">
<a class="btn" data-toggle="modal" href="#project-add-modal">Launch Modal</a>

      <div class="modal hide fade" id="project-add-modal" style="display: none; ">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">×</button>
          <h3>Add New Project</h3>
        </div>
        <div class="modal-body">
          <form class="form-horizontal">
            <fieldset>
              <div class="control-group">
                <label class="control-label" for="input01">Project Name</label>
                <div class="controls">
                  <input type="text" class="input-xlarge" id="project-name">
                </div>
              </div>
              <div class="control-group">
                <label class="control-label" for="textarea">Project Description</label>
                <div class="controls">
                  <textarea class="input-xlarge" id="project-desc" rows="3"></textarea>
                </div>
              </div>
              <label class="control-label">Invite People to
              collaborate on your project</label>
              <div class="control-group">
                <label class="control-label" for="prependedInput"></label>
                <div class="controls">
                  <div class="input-prepend">
                    <span class="add-on">@</span><input class="span2" id="prependedInput" size="16" type="text">
                  </div>
                  <p class="help-block">Here's some help text</p>
                </div>
              </div>
            </fieldset>
        </form></div>
        <div class="modal-footer">
          <a href="#" class="btn" data-dismiss="modal">Close</a>
          <a href="#" class="btn btn-primary">Save changes</a>
        </div>
      </div>

    </div></div>
<footer>

</footer>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/libs/jquery-1.7.2.min.js"><\/script>')</script>

<!-- scripts concatenated and minified via ant build script-->
<script src="js/bootstrap.min.js"></script>
<script src="js/plugins.js"></script>
<script src="js/script.js"></script>
<script src="cljs/client.js"></script><script type="text/javascript" src="deps.js"></script>
<script>$(function() {cljsbinding.boot()})</script>
<!-- end scripts-->


<script>
    var _gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview']];
    (function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];
    g.src=('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js';
    s.parentNode.insertBefore(g,s)}(document,'script'));
</script>




<iframe name="xpcpeer7X7y" id="xpcpeer7X7y" style="display: none; " src="http://localhost:9000/repl?xpc=%7B%22cn%22%3A%22PmCIncJdVC%22%2C%22tp%22%3Anull%7D"></iframe></body></html>

谢谢,穆尔塔萨

4

3 回答 3

50

我注意到在以下情况下可能会出现此问题:

  • JQuery 已加载两次。
  • 单独的 twitter 引导插件加载在 bootstrap.js 之上
  • .js 文件的包含顺序错误
  • 错误的类型被发送到函数。
于 2012-06-27T07:17:19.490 回答
2

确保你在做

$('#project-add-modal').modal("show") 

并不是

$("#myModal").modal("show") 

(因为上面的代码中不存在 myModal )

于 2012-06-27T08:43:46.747 回答
1

FWW 我遇到了类似的问题,这是因为我使用 JQuery 1.8.x 和 Bootrap 2.0.x。升级到 Bootstrap 2.1.x 节省了一天。

于 2012-09-03T16:47:04.207 回答