JQuery循环遍历外部div

时间:2011-06-17 23:18:05

标签: jquery html

<div id="container">
    <div rownumber="0" messageid="155" class="post" style="position: relative; top: 0px;">
        <div class="post_body"><span>asdfjhasf as dfkhgfk  dsfad fbd lfgldfgl dsgflag flugad luf gaduf ad fuilad lifg adslf gluad fluad fluiadlufg asu fglasd gfl</span></div>
        <div class="post_info"><ul><li class="vote" id="voteli">
        </div>
    </div>
</div>

所以容器div目前只有一个孩子,但是说它有10个或者什么,这个数字是任意的。我将如何遍历JQuery中的容器div,并为每个子项调用div class="post_body"上的JQuery插件。

3 个答案:

答案 0 :(得分:2)

像这样:

$("#container > div").each(function () {
    $("div.post_body", this).pluginMethod();
});

答案 1 :(得分:2)

从选择器返回的jQuery对象是一个匹配元素的数组。

由于您知道外部容器是什么,并且您知道要对该类的所有元素执行操作,因此您知道您的选择器。

$('#container .post_body').each(function(a,b) { });

此外,通常编写插件来迭代此数组。所以你应该能够做到:

$('#container .post_body').plugin();

答案 2 :(得分:1)

$('#container > .post > .post_body').pluginMethod();