Jquery load()加载超过我想要的

时间:2010-05-22 10:01:31

标签: javascript jquery

我正在尝试使用jquery函数将<div>的内容加载到同一页面上的另一个<div>中。但是当函数触发时,它会将整个<HTML>文档加载到指定的<div>中。知道为什么会这样做吗?我的代码如下:

Jquery的:

function button1() {
    $('#sidebar-content').fadeOut(function() {
        $(this).load('#button1').fadeIn();
    });
}
function button2() {
    $('#sidebar-content').fadeOut(function() {
        $(this).load('#button2').fadeIn();
    });
}

HTML:

<div id="content-holder"> 
    <div id="main-content" class="float-holder"> 
        <div id="inner">
            <h1>BRAND TRUTH</h1>
            <div id="flashcontent">
                <div id="button1">
                    <div id="content">
                        <h1>Brand Truth</h1>
                        <p>What this basically means is our way of working, the process involved by both ourselves and our client.</p>

                        <p>When the truth wheel process is followed, the end result is so much stronger.</p>
                    </div>
                </div>
                <div id="button2">
                    <div id="content">
                        <h1>Button 2 Content</h1>
                        <p>Some other content</p>

                        <p>Some other content x2</p>
                    </div>
                </div>
            </div>
            <script type="text/javascript">
                // <![CDATA[

                var so = new SWFObject("working.swf", "working", "400", "400", "9", "#FFFFFF");
                so.write("flashcontent");

                // ]]>
            </script>

        </div>
        <div id="sidebar">
            <div id="sidebar-content">
                Replace Content Here!
            </div>
        </div>
    </div><!-- end #main-content --> 
</div><!-- end #content-holder --> 

1 个答案:

答案 0 :(得分:5)

.load()是一个通过AJAX加载远程网页的功能,您想要的是使用.html(),如下所示:

function button1() {
    $('#sidebar-content').fadeOut(function() {
        $(this).html($('#button1').html()).fadeIn();
    });
}
function button2() {
    $('#sidebar-content').fadeOut(function() {
        $(this).html($('#button2').html()).fadeIn();
    });
}
没有参数的

.html()会在里面输入html,所以我们从按钮<div>获取它,然后.html(string)将html设置为内部,我们正在对结果进行处理