使用javascript onClick显示引导模式

Display Bootstrap Modal using javascript onClick

本文关键字:模式 显示 javascript onClick 使用      更新时间:2023-09-26

我需要能够使用onClick=""或类似功能打开Twitter引导模式窗口。只需要代码就可以进入onClick=""。我正在尝试制作一个可点击的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:

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

您不需要onclick。假设您使用的是引导3引导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

我正在寻找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>

必须首先创建一个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元素也不是很明智;像按钮这样的东西会更合适。

希望这能有所帮助!

我也遇到了同样的问题,经过大量的研究,我终于构建了一个js函数来根据我的需求动态创建模式。使用此功能,您可以在一行中创建弹出窗口,例如:

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

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

在上查找https://github.com/aybhalala/puymodals有关演示,请访问http://pateladitya.com/puymodals/