简体   繁体   English

在AJAX调用后用jquery刷新DOM

[英]Refresh DOM with jquery after AJAX call

I'm working on a new project http://www.hotwirerevealed.com which reveals / identifies hotels on hotwire.com. 我正在开发一个新项目http://www.hotwirerevealed.com ,该项目可以在hotwire.com上显示/识别酒店。 After inputting a state and a destination I have a javascript functions that uses jquery's .post method to post. 输入状态和目的地后,我有了一个使用jquery的.post方法发布的javascript函数。 The post request goes to an php page which outputs html, I se jquery's html method to place the content on the page. 发布请求转到输出html的php页面,我使用jquery的html方法将内容放置在页面上。

like so 像这样

function post(){
    $.post("lookup.php", {action: "find", area: area, stars: stars, amenities: amenities, state: $('#state').val()}, function(data){ 
        $("#details").html(data);
    });
}

I have hyperlinks to hotels which I like to use in a light box 我有超链接,指向要在灯箱中使用的酒店

<a class="hotel" href="http://google.com/search?btnI=1&amp;q=Amerisuites+Northeast+Orlando+(Airport+MCO)">Amerisuites Northeast</a>

im trying to use jquery's fancy box but fancy box 我试图使用jQuery的花哨的盒子,但花哨的盒子

$(document).ready(function(){
    $(".hotel").fancybox({
        'width'             : '75%',
        'height'            : '75%',
        'type'              : 'iframe'
    });
});

but it doesn't seem to work, im guessing because jquery doesn't know the element it there? 但它似乎不起作用,我猜测是因为jquery不知道那里的元素? I've tried to use jquery live() method with little success, any help would be appreciated, thanks ahead of time 我尝试使用jquery live()方法收效甚微,不胜感激,谢谢

The quickest way I'd do it is to add the livequery plugin to the page, 我最快的方法是将livequery插件添加到页面中,
and then instead of your: 然后代替您的:

$(document).ready(function(){
  $(".hotel").fancybox({
        'width'             : '75%',
        'height'            : '75%',
        'type'              : 'iframe'
  });
});

do this: 做这个:

$(document).ready(function(){
  $(".hotel").livequery(function(){
    $(this).fancybox({
        'width'             : '75%',
        'height'            : '75%',
        'type'              : 'iframe'
      });
  });
});

Just to be clear, $("#details").html(data) loads the "a" tags right? 为了清楚起见,$(“#details”)。html(data)加载“ a”标签对吗?

Last time I checked, fancybox() requires the element to be already existing on the DOM. 上次我检查时,fancybox()要求该元素已经在DOM上存在。 When $(document).ready fires, the $(".hotel") probably doesn't exist yet. 当$(document).ready触发时,$(“。hotel”)可能还不存在。 You can move the fancybox setups after $.post like: 您可以在$ .post之后移动fancybox设置,例如:

function post(){
  $.post("lookup.php", 
    {action: "find", area: area, stars: stars, amenities: amenities, state:
      $('#state').val()}, 
    function(data) { 
      $("#details").html(data);
      // bind fancy box
      $(".hotel").fancybox({
        'width'             : '75%',
        'height'            : '75%',
        'type'              : 'iframe'
      });
  });
}

You might have to watch out for calling fancybox to elements that have been called with fancybox already. 您可能需要提防将fancybox调用到已经用fancybox调用的元素。 There might be some complications there. 那里可能会有一些并发症。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM