在ajax请求之后无法从jquery对象获取div内容

时间:2011-04-28 09:28:48

标签: jquery html

这是我的jquery:

$.ajax({
url: hash,
    success: function(data){
        var thePage = $(data);
        alert(data);
        $('#slider_menu').html($('div#menu_bg', thePage).html());
    }
});

警报(数据)返回:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <title>Pharma Mix</title>
    <link href="style.css" rel="stylesheet" type="text/css">
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
        var currentX = 0;
        var currentY = 0;
        var pixelIncrement = 39;
        var currentFrame = 0;
        var currentImage = '';
        $('#rotator').mousemove(function(e){
            currentY = e.clientY - this.offsetTop;
            currentX = e.clientX - this.offsetLeft;
            currentFrame = (parseInt(currentX / pixelIncrement) + 1);
                //$(this).html(currentFrame);
                if(currentFrame < 12)
                {
                currentImage = String(currentFrame) + ".jpg" 
                $(this).css({'backgroundImage':'url("images/' + currentImage + '")'});
                }
        });
     });    
    </script>
</head>

<body>

    <div id="menu_bg">
        <div id="wrapper">       
            <div style="height:30px; background:#009de4"></div>
            <div id="homepage_menu">
                <div style="float:left"><br><a href="#healthcare.php"><img src="images/hcpbutton.jpg" style="margin-right:25px"></a><a href="#pharma.php"><img src="images/pharmabutton.jpg"></a></div>
                <div style="float:right"><br><img src="images/pharmamixlogo.jpg" alt=""></div>  
            </div> <!-- #homepage_menu -->                   
        </div> <!-- #wrapper -->  
    </div> <!-- #menu_bg -->        


    <div id="homepage_bg"> 

        <div id="homepage_wrapper">

            <div style="text-align:center"><img src="images/homepage_heading.jpg" alt=""></div>

            <div id="homepage_leftboxes">
                <div class="homepage_box1">
                    <p class="homepage_boxtext">
                    "Thanks for delivering an outstanding event. Feedback has been excellent all round - in terms of programme, venue, ambiance, technical delivery and the conference team."
                    <br><br>
                    <b>Tracey Guise</b>, BSAC
                    </p>
                </div> <!-- #homepage_box1 -->              
                <div class="homepage_box3">
                    <p class="homepage_boxtext">
                    The whole event ran very well - Debbie did an excellent job of keeping it all running very smoothly indeed.
                    <br><br>
                    <b>Anthony Grosso</b>, UCLH
                    </p>
                </div> <!-- #homepage_box3 -->
            </div> <!-- #homepage_leftboxes -->         

            <div id="rotator" style="width:430px;height:336px; float:left;" class="initalise"></div>

            <div id="homepage_rightboxes">            
                <div class="homepage_box2">
                    <p class="homepage_boxtext">
                    "The understanding and creative thinking that Pharma Mix have demonstrated has provided some genuine and demonstratable return on investment to the Evolution brand."<br><br>
                    <b>Luke Rudman</b>, Evolution Homecare
                    </p>
                </div> <!-- #homepage_box2 -->
                <div class="homepage_box4">
                    <p class="homepage_boxtext">
                    "I would like to thank Debbie & her team for the excellence in the delivery of this superb website & her patience with us."
                    <br><br>
                    Kevin Palmer, Mundipharma
                    </p>                        
                </div> <!-- #homepage_box4 -->

            </div> <!-- #homepage_rightboxes -->  

            <div style="height:170px;"><img src="images/homepage_clients.jpg"></div>

        </div><!-- #homepage_wrapper --> 

    </div><!-- #homepage_bg --> 

    <div style="color:white; text-align:center; padding-top:30px">Contact Us: 28b Priestgate  .  Peterborough  .  PE1 1JA  .  UK   Tel: 01733 554472</div>

</body>
</html> 

#menu_bg div不会返回其内容! :(

对于嵌入到文档中的任何div,这个工作(例如,这将有效),但我对所请求页面的内容没有任何发言权。

为什么这不起作用的任何想法?或者如何让它发挥作用?

谢谢

5 个答案:

答案 0 :(得分:1)

你能试试吗?

   $.ajax({
    url: hash,
        success: function(data){
            $('#slider_menu').html($(data).filter('div#menu_bg').html());
        }
    });

希望这有帮助。

答案 1 :(得分:0)

如何找到div#wrapper的父级?

答案 2 :(得分:0)

您可以尝试从body代码开始。

$("body").next()

next()为您提供即时标记。

答案 3 :(得分:0)

必须更改返回的代码......没有答案 - 它必须是一个bug或类似的。

答案 4 :(得分:0)

尝试

$('#slider_menu').html($(data).find('div#menu_bg').html());

干杯!