【问题标题】:Contents inside Popup Modal not showing up弹出模式中的内容未显示
【发布时间】:2012-12-27 11:34:52
【问题描述】:

我使用this Tutorial 在页面加载时显示弹出式模态窗口,由于某种原因,它没有显示模态内的内容和图像。

我正在处理的代码位于jsFiddle

我想要一个简单的 Modal 弹出窗口,我可以在上面显示不同维度的图像形式的消息,我希望 Modal 显示在页面中间并根据图像调整宽度和高度。在这方面我将不胜感激。

代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Simple JQuery Modal Window from Queness</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
<script>
$(document).ready(function() {
        var id = $(this).attr('href');
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set height and width to mask to fill up the whole screen
        $('#mask').css({'width':maskWidth,'height':maskHeight});

        //transition effect    
        $('#mask').fadeIn(1000);   
        $('#mask').fadeTo("slow",0.8);

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window to center
        $(id).css('top',  winH/2-$(id).height()/2);
        $(id).css('left', winW/2-$(id).width()/2);

        //transition effect
        $(id).fadeIn(2000);

    //if close button is clicked
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();
        $('#mask, .window').hide();
    });    

    //if mask is clicked
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });        

    $(window).resize(function () {

        var box = $('#boxes .window');

        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set height and width to mask to fill up the whole screen
        $('#mask').css({'width':maskWidth,'height':maskHeight});

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window to center
        box.css('top',  winH/2 - box.height()/2);
        box.css('left', winW/2 - box.width()/2);

    });    
});
</script>
<style>

/* Z-index of #mask must lower than #boxes .window */
#mask {
  position:absolute;
  z-index:9000;
  background-color:#000;
  display:none;
}
#boxes .window {
  position:fixed;

  display:none;
  z-index:9999;
  padding:20px;
}
/* Customize your modal window here, you can add background image too */
#boxes #dialog {

}
</style>

</head>
<body>

<div id="boxes">
    <!-- #customize your modal window here -->
    <div id="dialog" class="window">
        <b>Testing of Modal Window</b> |
        <!-- close button is defined as close class -->
        <a href="#" class="close">Close it</a>
 <br />
        <img src="http://pierre.chachatelier.fr/programmation/images/mozodojo-original-image.jpg" />
    </div>
    <!-- Do not remove div#mask, because you'll need it to fill the whole screen -->
    <div id="mask"></div>
</div>

</body>
</html>

【问题讨论】:

    标签: javascript jquery jquery-ui simplemodal


    【解决方案1】:

    错误是这一行操作

        var id = $(this).attr('href');
    

    在 document.ready 上,您没有 href 属性,但如果您将其替换为

        var id = "#dialog";
    

    您的模态将起作用。见:http://jsfiddle.net/HJUZm/3/

    更多...

    对于您正在阅读的教程,我可以看到 2 个可能的错误。

    首先是你的 HTML 中你缺少

        <!-- #dialog is the id of a DIV defined in the code below -->
        <a href="#dialog" name="modal">Simple Modal Window</a>
    

    由于某种原因,您忘记使用$('a[name=modal]').click(function(e) {,而是将其插入到 document.ready 中

    $('a[name=modal]') 之间的区别在于 document.ready 在页面准备就绪时触发一次,$('a[name=modal]').click(function(e){...});name 属性为modal 的每次点击时触发

    【讨论】:

    • 我同意你的看法,但我修改了脚本以处理页面加载事件,而不是有人会点击链接&lt;a href="xxx" id=xx&gt; Click Here&lt;/a&gt;。感谢您的帮助.. 我是否需要对脚本进行更多更改以使其按我的意图工作。
    • 我不这么认为。对于你来说,你可以看看 4. header in your tutorial。 queness.com/post/77/simple-jquery-modal-window-tutorial他为您提供了一个示例。 :-)
    猜你喜欢
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    相关资源
    最近更新 更多