如何在Jquery的加载方法中应用JS数据?

时间:2011-09-01 21:10:57

标签: javascript jquery

以下是JS的片段:

...
$(this).load('html_fragment.html', function(data) {
    console.log(data);
    $(data).find('.accordion').accordion();
});
...

这是我正在加载的文档的内容(html_fragment.html):

<form>
    <div class="accordion">
        <h3><a href="#">Section 1</a></h3>
        <div>
            <p>
                Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer
                ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit
                amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut
                odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.
            </p>
        </div>
        <h3><a href="#">Section 2</a></h3>
        <div>
            <p>
                Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet
                purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor
                velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In
                suscipit faucibus urna.
            </p>
        </div>
        <h3><a href="#">Section 3</a></h3>
        <div>
            <p>
                Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis.
                Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero
                ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis
                lacinia ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui.
            </p>
            <ul>
                <li>List item one</li>
                <li>List item two</li>
                <li>List item three</li>
            </ul>
        </div>
        <h3><a href="#">Section 4</a></h3>
        <div>
            <p>
                Cras dictum. Pellentesque habitant morbi tristique senectus et netus
                et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in
                faucibus orci luctus et ultrices posuere cubilia Curae; Aenean lacinia
                mauris vel est.
            </p>
            <p>
                Suspendisse eu nisl. Nullam ut libero. Integer dignissim consequat lectus.
                Class aptent taciti sociosqu ad litora torquent per conubia nostra, per
                inceptos himenaeos.
            </p>
        </div>
    </div>
</div><!-- end accordion -->


    <div id="horizontal">
        <p>horizontal test data</p>
    </div><!-- end horizontal -->
</form>

问题?:Jquery UI手风琴功能在加载后似乎对包装对象无效。有没有办法在回调期间对返回的html片段执行JS?

1 个答案:

答案 0 :(得分:1)

当你到达行时

$(data).find('.accordion').accordion();

是DOM中的html片段吗?如果是这样,那么就不要在数据中找到accordinon,在DOM中找到它

$('body').find('.accordion').accordion();

使用身体或任何容器。