如何处理Ajax响应中返回的html? [英] How do I manipulate html returned in an ajax response?

查看:277
本文介绍了如何处理Ajax响应中返回的html?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索并尝试了很多方法,但是根本无法使其正常工作.我有以下内容:

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)输出g.html中包含的html.然后,我尝试读取id ='baa'的内容,并将其移入主文档中的id ='result'中.我的最终目标是在将某些数据放入其中之前对其进行操作,但是我无法使这个简单的版本正常工作.

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看起来像这样....

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>Testing</title>
</head>
<body>
    <div id='baa'>
        <ul>
            <li>list 1</li>
            <li>list 2</li>
            <li>list 3</li>
        </ul>
    </div>
</body>
</html>

如果执行 alert(html),我将获得完整的html输出,如果尝试对newData做任何事情,则会得到 [object Object] .

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!

推荐答案

尝试...

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

这篇关于如何处理Ajax响应中返回的html?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆