使用JavaScript onClick显示Bootstrap模态框

36

我需要能够使用 onClick="" 或类似的函数打开 Twitter Bootstrap 模态窗口。只需要将代码放入 onClick="" 中即可。我正在尝试制作一个可点击的 div 来打开模态窗口。

代码片段:

Div 代码:

<div class="span4 proj-div" onClick="open('GSCCModal');">

模态框代码:

<div id="GSCCModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

Javascript:


JavaScript:
function open(x){
    $(x).modal('show');
}

1
使用 onClick 很不受欢迎。你至少不能在 <script> 标签中包含一个脚本吗? - James Lai
2
你为什么不能使用内置的Bootstrap功能呢?(例如:在你的div上放置data-toggle="modal"和data-target="#foo"?) - Lola
我正在使用onClick来避免在div周围使用链接。这些是代码摘录,不是它们在代码中的实际样子。@BriAnna 你是指非模态div代码吗?我没有意识到这会有任何作用,或者我甚至可以这样做。 - Snappawapa
1
是的。任何元素都可以成为可点击触发器。只需将这两个属性添加到您的div中即可。并不需要a标签。但是您的标记不正确。您必须遵循Twitter Bootstrap的示例。 - Lola
4个回答

47

你不需要使用 onclick。假设你正在使用Bootstrap 3 Bootstrap 3文档

<div class="span4 proj-div" data-toggle="modal" data-target="#GSCCModal">Clickable content, graphics, whatever</div>

<div id="GSCCModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
 <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;  </button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

如果您使用的是Bootstrap 2,您需要按照这里的标记:http://getbootstrap.com/2.3.2/javascript.html#modals


先生,那正是我所需要的。我目前正在使用Bootstrap 2.3.2,但它仍然完美地运行!谢谢! - Snappawapa
如果我需要在另一个按钮点击时打开模型怎么办? - always-a-learner
我明白了。如何使用jQuery打开Bootstrap模态窗口? - always-a-learner

15

我正在寻找onClick选项,以根据列表中的项目设置模态框的标题和正文。T145的答案帮了我很多,所以我想分享一下我是如何使用它的。

确保包含JavaScript函数的标签类型为text/javascript,以避免冲突:

<script type="text/javascript"> function showMyModalSetTitle(myTitle, myBodyHtml) {

   /*
    * '#myModayTitle' and '#myModalBody' refer to the 'id' of the HTML tags in
    * the modal HTML code that hold the title and body respectively. These id's
    * can be named anything, just make sure they are added as necessary.
    *
    */

   $('#myModalTitle').html(myTitle);
   $('#myModalBody').html(myBodyHtml);

   $('#myModal').modal('show');
}</script>

现在可以在例如按钮之类的元素内的onClick方法中调用此函数:

<button type="button" onClick="javascript:showMyModalSetTitle('Some Title', 'Some body txt')"> Click Me! </button>

0

我曾经遇到过同样的问题,经过大量研究后,我最终构建了一个JS函数来根据我的需求动态创建模态框。使用这个函数,你可以用一行代码创建弹出窗口,例如:

puyModal({title:'Test Title',heading:'Heading',message:'This is sample message.'})

或者您可以使用其他复杂的功能,例如iframe、视频弹出窗口等。

https://github.com/aybhalala/puymodals上找到它。要查看演示,请转到http://pateladitya.com/puymodals/


0

首先必须创建一个 JavaScript 函数,用于保存你想要执行的操作:

function print() { console.log("Hello World!") }

然后该函数必须在元素内的onClick方法中调用:

<a onClick="print()"> ... </a>

您可以从Bootstrap 3文档http://getbootstrap.com/javascript/#modals直接了解有关模态交互的更多信息。

您的模态绑定也不正确。它应该像这样,其中"myModal"=元素的ID:

$('#myModal').modal(options)

换句话说,如果你真的想保留你已经有的东西,在 GSCCModal 前面加上 "#",看看是否有效。
将 onClick 绑定到 div 元素上也不是很明智;像按钮之类的东西更合适。
希望这可以帮助你!

不幸的是,这并不能解决问题。即使使用 Twitter Bootstrap 函数,我最远只能得到一个空白屏幕。我还将更新我的初始问题,附上我目前正在使用的代码。 - Snappawapa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接