开发者

How do I manipulate html returned in an ajax response?

I have searched and tried a number of things for this, but simply cannot get this to work. I have the following:

$.ajax({
    url: 'g.html',
    type: 'GET',
    dataType: 'html',
    timeout: 4000,
    cache: false,
    error: function(request, status, error){ alert('Error'); },
    success: function(html){
        alert(html);
        var newData=$('#baa', html);
        $('#result').html('Blah'+newData);
    }
});

alert(html) outputs the html contained in g.html. I am then trying to read in the contents of id='baa' and move that into id='result' in the main document. My end goal is to manipulate some of that data before it is placed there, but I can't get this simple version working.

g.html looks like this....

<!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>T开发者_Go百科esting</title>
</head>
<body>
    <div id='baa'>
        <ul>
            <li>list 1</li>
            <li>list 2</li>
            <li>list 3</li>
        </ul>
    </div>
</body>
</html>

I get the full html output if I do alert(html), if I try and do anything with newData I get [object Object].

Completely stumped, any help really appreciated!


try...

success: function(html){
            var $html = $(html);
            $('#result').html($html.filter('#baa').html());         
         }


Try:

var newData=$(html).find('#baa');
$('#result').html('Blah'+newData.html());

Also, worth trying

alert(newData.html());

...to see more of what's happening (that should prevent the [object object] output).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜