jQuery 无法在 chrome 中使用 javascript 调用引导模式窗口(在 firefox 中工作)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11220670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
unable to call bootstrap modal window using javascript in chrome (works in firefox)
提问by murtaza52
I have included the sample code from twitter-bootstrap for a modal window. If I click on the button it opens up the modal window. However if I try to show the modal window through javascript I get the following error -
我已经包含了来自 twitter-bootstrap 的模式窗口的示例代码。如果我点击按钮,它会打开模态窗口。但是,如果我尝试通过 javascript 显示模态窗口,则会出现以下错误 -
$("#myModal").modal("show")
TypeError: Object [object Object] has no method 'modal'
This error is only coming in chrome and works in firefox.
此错误仅出现在 chrome 中,并且在 Firefox 中有效。
Below is the HTML. "project-add-modal" is the id of the modal box. Navigate to Project -> Add to see the modal button. -
下面是 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>
Thanks, Murtaza
谢谢,穆尔塔扎
回答by baptme
I noticed this problem may occur under the following conditions:
我注意到在以下情况下可能会出现此问题:
- JQuery has been loaded twice.
- The individual twitter bootstrap plugin is loaded on top of bootstrap.js
- The .js files are included in the wrong order
- The wrong type is sent to the function.
- JQuery 已加载两次。
- 单独的 twitter bootstrap 插件加载在 bootstrap.js 之上
- .js 文件包含在错误的顺序中
- 错误的类型被发送到函数。
回答by periklis
Make sure you're doing
确保你在做
$('#project-add-modal').modal("show")
and not
并不是
$("#myModal").modal("show")
(since myModal doesn't exist in your code above)
(因为上面的代码中不存在 myModal)
回答by David Dossot
FWW I had a similar problem and it was due to the fact I was using JQuery 1.8.x with Bootrap 2.0.x. Upgrading to Bootstrap 2.1.x saved the day.
FWW 我遇到了类似的问题,这是因为我将 JQuery 1.8.x 与 Bootrap 2.0.x 一起使用。升级到 Bootstrap 2.1.x 挽救了这一天。