从ajax响应创建分组div的变量

时间:2013-04-22 22:03:43

标签: jquery ajax find

好吧,这让我感到困惑。

我正在对get_post.php页面进行ajax调用。在那个页面上,我从各种数组创建html。如果我要查看这个get_post.php页面的来源,我会看到基本的html,未经过固定化。

所以在我的ajax响应中我得到的只是html,这就是我需要的。现在我遇到了问题。

在get_post.php页面上,我有各种div。

<div class="pictures">this div has inner elements</div>
<div class="text">this div has inner elements</div>
<div class="video">this div has inner elements</div>

我会有很多每种类型的div。我可以有10个div与类图片,10个与类视频和200与文本。这些div中的每一个都是内容的包装器。但每个都根据其内容进行了风格化。

我的计划是,当ajax成功将所有div类型存储到变量时。

var pictures = <div class="pictures">this div has inner elements</div><div class="pictures">this div has inner elements</div><div class="pictures">this div has inner elements</div> etc etc

var text = <div class="text">this div has inner elements</div><div class="text">this div has inner elements</div><div class="text">this div has inner elements</div> etc etc

var videos = <div class="video">this div has inner elements</div><div class="video">this div has inner elements</div><div class="video">this div has inner elements</div> etc etc

我需要变量中的所有div,所以在点击时我可以将它们附加到列表中。

$('.textLink').click(function() {
$(html).append(text);
});

这会将所有文本div添加到最后。

我遇到的麻烦是设置所有div及其内容的变量。我怎么能这样做?

这是我的ajax

$.ajax({
    type: 'GET',
    url: 'interactions/get_posts.php',
    dataType: 'json',
    async: true,
    cache: false,
    success: function(results) {
        var data = results.responseText;
        var pictures = ????
        var text = ????
        var videos = ????

    },
    error: function(results) {

    }
});

以及我的ajax响应可能是什么样的

    <div class="pictures">this div has inner elements</div>
<div class="text">this div has inner elements</div>
<div class="pictures">this div has inner elements</div>
<div class="video">this div has inner elements</div>
<div class="video">this div has inner elements</div>
<div class="pictures">this div has inner elements</div>
<div class="video">this div has inner elements</div>
<div class="pictures">this div has inner elements</div>
<div class="text">this div has inner elements</div>
<div class="pictures">this div has inner elements</div>
<div class="video">this div has inner elements</div>
<div class="pictures">this div has inner elements</div>
<div class="text">this div has inner elements</div>
<div class="text">this div has inner elements</div>
<div class="text">this div has inner elements</div>

我觉得这很容易做到,但我已经在循环中走了这么久我被卡住了......

0 个答案:

没有答案