【问题标题】:Bootstrap modal not defined when trying to present it by javascript尝试通过 javascript 呈现时未定义引导模式
【发布时间】:2017-07-10 08:45:36
【问题描述】:

所以,我正在开发一个<canvas> 游戏,它是在引导模式中创建和呈现的。我正在尝试编写一个将在 gameOver 上调用的 javascript 函数,它将在我的画布顶部呈现一个新的(堆叠的)模式,并带有 3 个用于提交/注册/重播的按钮。

我在我的 html 中通过 data-toggle 呈现我的第一个模态并且工作正常,但是当我尝试在顶部呈现我的第二个模态(通过 javascript)时,它似乎被报告为未定义。

这是我的代码:JSFiddle
(在这种情况下,我以我的画布绘制方法中的第二个模式为例,但我也尝试了一个单独的按钮、其他事件侦听器等。问题总是一样的)。

如果有更好的方法来显示带有按钮的 Game Over 屏幕,我愿意接受建议。我想避免在我的画布上绘制任何按钮并获得坐标来读取它的点击。

提前致谢。

【问题讨论】:

    标签: javascript html twitter-bootstrap modal-dialog


    【解决方案1】:

    如果你想在另一个模态之上打开模态,你必须调整z-index。使用此处Multiple modals overlay 提供的解决方案,您可以执行类似的操作。

    $(document).ready(function() {
    
        let canvas = null;
        let drawCanvas = () => {
            canvas = document.getElementById("myCanvas");
            const context = canvas.getContext("2d");
            context.fillStyle = "#FF0000";
            context.fillRect(0, 0, 630, 850);
    
        };
        $('#openBtn').click(function() {
            $('#myModal').modal({
                show: true
            });
            if (!canvas) {
                drawCanvas();
            }
            setTimeout(function() {
                $('#myModal2').modal({
                    show: true,
                });
            }, 2000)
        });
    
        $(document).on('show.bs.modal', '.modal', function(event) {
            var zIndex = 1040 + (10 * $('.modal:visible').length);
            $(this).css('z-index', zIndex);
            setTimeout(function() {
                $('.modal-backdrop').not('.modal-stack').css('z-index', zIndex - 1).addClass('modal-stack');
            }, 0);
        });
    
    
    });
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>JS Bin</title>
    </head>
    <body>
      <h2>Stacked Bootstrap Modal Example.</h2>
     <a id="openBtn" class="btn btn-primary">Launch modal</a>
    
    <div class="modal fade" id="myModal">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    	<h4 class="modal-title">Modal 1</h4>
    
                </div>
                <div class="modal-body">	
    				          <canvas id="myCanvas" width="630" height="850">
              </canvas>
    				<a data-toggle="modal" href="#myModal2" class="btn btn-primary">Launch modal</a>
    
                </div>
                <div class="modal-footer">	<a href="#" data-dismiss="modal" class="btn">Close</a>
    	<a href="#" class="btn btn-primary">Save changes</a>
    
                </div>
            </div>
        </div>
    </div>
    <div class="modal fade rotate" id="myModal2">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    	<h4 class="modal-title">Modal 2</h4>
    
                </div>
                <div class="container"></div>
                <div class="modal-body">Content for the dialog / modal goes here.
                    <br>
                    <br>
                    <p>come content</p>
                    <br>
                    <br>
                    <br>	<a data-toggle="modal" href="#myModal3" class="btn btn-primary">Launch modal</a>
    
                </div>
                <div class="modal-footer">	<a href="#" data-dismiss="modal" class="btn">Close</a>
    	<a href="#" class="btn btn-primary">Save changes</a>
    
                </div>
            </div>
        </div>
    </div>
    <div class="modal fade" id="myModal3">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    	<h4 class="modal-title">Modal 3</h4>
    
                </div>
                <div class="container"></div>
                <div class="modal-body">Content for the dialog / modal goes here.
                    <br>
                    <br>
                    <br>
                    <br>
                    <br>	<a data-toggle="modal" href="#myModal4" class="btn btn-primary">Launch modal</a>
    
                </div>
                <div class="modal-footer">	<a href="#" data-dismiss="modal" class="btn">Close</a>
    	<a href="#" class="btn btn-primary">Save changes</a>
    
                </div>
            </div>
        </div>
    </div>
    <div class="modal fade" id="myModal4">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    	<h4 class="modal-title">Modal 4</h4>
    
                </div>
                <div class="container"></div>
                <div class="modal-body">Content for the dialog / modal goes here.</div>
                <div class="modal-footer">	<a href="#" data-dismiss="modal" class="btn">Close</a>
    	<a href="#" class="btn btn-primary">Save changes</a>
    
                </div>
            </div>
        </div>
    </div>	
    <script src="https://code.jquery.com/jquery.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    
    </body>
    </html>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-15
      • 2015-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多