【问题标题】:Modal dismiss not working after load function加载功能后模态关闭不起作用
【发布时间】:2016-09-29 01:03:48
【问题描述】:

我一直在寻找解决方案,但似乎没有任何解决方案。

我的代码中有一个模态加载函数脚本,并且模态显示正常,但是当我尝试使用代码中的一个 html 按钮从 data-dismiss="modal" 指令关闭模态时,它不关闭模式,但如果我点击“打开模式”按钮,那么两个关闭按钮工作正常:

<script>
setTimeout(function(){
$(window).load(function(){
        $('#myModal').modal('show');
},3000);
</script>
<div class="container"><!-- Trigger the modal with a button --> <button class="btn btn-info btn-lg" type="button" data-toggle="modal" data-target="#myModal">Open Modal</button> <!-- Modal -->
<div id="myModal" class="modal fade" tabindex="-1">
<div class="modal-dialog"><!-- Modal content-->
<div class="modal-content">
<div class="modal-header"><button class="close" type="button" data-dismiss="modal">&times;</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer"><button class="btn btn-default" type="button" data-dismiss="modal">Close</button></div>
</div>
</div>
</div>
</div>

所以 data-dismiss="modal" 仅在我从“打开模式”按钮手动加载模式时有效,但当我使用加载功能在页面加载时显示它时不起作用。

我将不胜感激,欢迎提出任何建议,

谢谢。

【问题讨论】:

    标签: javascript modal-dialog bootstrap-modal


    【解决方案1】:

    $(window).load(function(){
        setTimeout(function(){
            $('#myModal').modal('show');
        },3000)
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    
    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
    
    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <div class="container"><!-- Trigger the modal with a button --> <button class="btn btn-info btn-lg" type="button" data-toggle="modal" data-target="#myModal">Open Modal</button> <!-- Modal -->
    <div id="myModal" class="modal fade" tabindex="-1">
    <div class="modal-dialog"><!-- Modal content-->
    <div class="modal-content">
    <div class="modal-header"><button class="close" type="button" data-dismiss="modal">&times;</button>
    <h4 class="modal-title">Modal Header</h4>
    </div>
    <div class="modal-body">
    <p>Some text in the modal.</p>
    </div>
    <div class="modal-footer"><button class="btn btn-default" type="button" data-dismiss="modal">Close</button></div>
    </div>
    </div>
    </div>
    </div>

    【讨论】:

    • 您好 SaJed,感谢您的帮助,我已经测试了您发送给我的脚本,但仍然无法正常工作,您认为我应该在脚本中以某种方式实例化关闭按钮吗?因为它在使用加载功能加载时似乎没有链接到模态,但在使用触发按钮加载时工作完美。
    • 我已经测试了 sn-p 并且它可以工作我什至在括号上测试它,它加载正常,但我认为它在我的环境中不起作用,因为我被限制使用没有的 html 编辑器允许我使用 body 或 head 标签来嵌入 css 样式表,因此无论如何它都会加载模态,我必须使用 Style display:none 来加载模态,但是现在当我单击加载按钮时,它似乎加载好的,但没有显示模态对话框类的背景图像。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多