donglong2856 2010-04-18 01:23
浏览 277
已采纳

在AJAX调用后使用jquery刷新DOM

I'm working on a new project http://www.hotwirerevealed.com which reveals / identifies hotels on hotwire.com. After inputting a state and a destination I have a javascript functions that uses jquery's .post method to post. The post request goes to an php page which outputs html, I se jquery's html method to place the content on the page.

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

$(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? I've tried to use jquery live() method with little success, any help would be appreciated, thanks ahead of time

  • 写回答

2条回答 默认 最新

  • douzhi7070 2010-04-18 01:38
    关注

    Just to be clear, $("#details").html(data) loads the "a" tags right?

    Last time I checked, fancybox() requires the element to be already existing on the DOM. When $(document).ready fires, the $(".hotel") probably doesn't exist yet. You can move the fancybox setups after $.post like:

    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. There might be some complications there.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏