【问题标题】:modal window will not open模态窗口打不开
【发布时间】:2012-04-12 11:22:25
【问题描述】:

我正在尝试向我的网页添加模式窗口选项。这是一个搜索页面,当单击每个搜索结果时,链接将在模式窗口中打开。所以我使用了页面 http://www.queness.com/post/77/simple-jquery-modal-window-tutorial 中的代码,除了 jquery 模板部分之外,模式窗口在页面中的任何地方都会出现。我的模板编码是这样的

<script id="srch1" type="text/x-jquery-tmpl"> 
        <tr><td><div id="title"><a href="#dialog" name="modal" style="text-decoration:none; color:#333333;"><b>${_source.Title}</b></a></div></td></tr>
        <tr><td><div id="date">${_source.fetchTimeStamp}&nbsp;|&nbsp; ${_source.SourceName}</div></td></tr>
        <tr><td><div id="content">${_source.Content}</div></td></tr>
</script>

所以当我们点击标题链接时,页面不会在模式窗口中打开,而是返回到顶部,并且“#dialog”会出现在 webapge 地址旁边。怎么解决这个问题?

Javascript/jQuery 编码是这样的

$(document).ready(function() {

//select all the a tag with name equal to modal
$('a[name=modal]').click(function(e) {
    //Cancel the link behavior
    e.preventDefault();

    //Get the A tag
    var id = $(this).attr('href');

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

    //Set heigth 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').hide();
    $('.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);

});

});

和纯 HTML 部分

<div id="boxes">
<div id="dialog" class="window">
Simple Modal Window | 
<a href="#" class="close"/>Close it</a>
</div>
 <div id="mask"></div>
</div>

【问题讨论】:

  • 代码很好......它在 部分内工作! bt 当我将链接放在 jquery 模板中时..它不会打开..
  • 您发布的代码和链接相当混乱...尝试将其归结为不起作用的部分。你的链接是否有一个属性name,值为modal,点击它时是否加载了jquery?
  • 我只粘贴了相关部分。我会解释。我添加的 html 部分用于模式窗口。它们是隐藏的,只有在单击搜索结果链接时才会显示。因此,一旦我输入了一个搜索词,我就会得到搜索结果,然后单击每个链接会激活一个显示该链接内容的模式窗口。第一个脚本部分是 jquery 模板显示包含搜索结果的数组..
  • 是的,我有一个名为 modal 的属性。它是我粘贴的第一个代码...
  • 我注意到您缓存了元素的“id”值。为什么不将 $(id) 也存储在本地变量中,而不是每次都将其包装起来(进行 DOM 调用)?

标签: jquery html jquery-templates


【解决方案1】:

您的代码看起来不错,我将它粘贴到 JsFiddle 中,它的行为应该是这样的:http://jsfiddle.net/3Dqen/

你能试试看有没有 JS 错误吗?


可能是dom加载后插入的链接,所以没有列出。尝试使用 LIVE 功能。

$('a[name=modal]').live("click", function(){
    //Cancel the link behavior
    e.preventDefault();

    //Get the A tag
    var id = $(this).attr('href');

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

    //Set heigth 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); 
});

【讨论】:

  • 是的..它工作正常......但是当我将它们链接到 head 标签中的 jquery 模板中时它不会工作..
  • 这是我提供链接的地方
  • 如果将其输出到浏览器,则模板本身似乎存在问题。你在调用$.template(...); 函数吗?如果输出错误,要么没有编译,要么编译时出错。
  • 我正在使用 .tmpl() 函数并将其附加到 div 中,我得到了输出。当我们将鼠标移到每个标题上时,它会将光标显示为“手”,因为它是一个链接..但是当我们单击链接时,模式窗口将不会打开,而是因为我将 href 指定为“#dialog” , #dialog 附加到地址栏中的网页地址...
  • 哈哈,太好了!猜猜 jQuery 确实将 tmpl 加载到 dom 中,很奇怪! :)
猜你喜欢
  • 2016-09-17
  • 2018-06-04
  • 2018-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多