Popup Modal中的内容未显示

时间:2012-12-27 11:34:53

标签: javascript jquery jquery-ui simplemodal

我正在使用this Tutorial在页面加载时显示弹出模式窗口,由于某种原因它没有显示内容和&莫代尔内部的形象。

我正在处理的代码位于jsFiddle

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

CODE

<!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>

1 个答案:

答案 0 :(得分:0)

错误就是这一行op

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

在document.ready上你没有href属性,但如果你把它替换为

    var id = "#dialog";

你的模态会起作用。请参阅:http://jsfiddle.net/HJUZm/3/

更多...

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

首先,你的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 / p>