如何使用.load()只从另一个html文件加载特定的div

时间:2013-11-08 07:41:17

标签: html ajax jquery

我有一个html文件,我正在使用.load() div photo_index的某些部分从另一个html文件加载到 div profile_wrapper_container_FormLogs)在我原来的html中。它确实加载了html文件。但它加载整个html文件,而不仅仅是div。如何从另一个html加载所需的div,即photo_index?任何帮助都感激不尽。谢谢!

HTML:

            <div id="profile_leftbar">

                <div id="profile_wrapper_container_summary">
                    <div id="profile_container_summary">
                        <div id="profile_summary">
                            <div id="profile_box1" class="box">
                                <a href="kkh.html" id="photo"><p>Photo</p>
                                    <p>450</p></a>
                            </div>
                            <div id="profile_box2" class="box">
                                <a href="#" id="video"><p>Video</p>
                                    <p>132</p></a>
                            </div>
                            <div id="profile_box3" class="box">
                                <a href="#" id="page"><p>Page</p>
                                    <p>14</p></a>
                            </div>
                            <div id="profile_box4" class="box">
                                <a href="#" id="forum"><p>Forum</p>
                                    <p>51</p></a>
                            </div>
                            <div id="profile_box5" class="box">
                                <a href="#" id="followers"><p>Followers</p>
                                    <p>551</p></a>
                            </div>
                            <div id="profile_box6" class="box">
                                <a href="#" id="following"><p>Following</p>
                                    <p>317</p></a>
                            </div>
                            <span class="clear_left"></span>
                        </div>
                        <div id="profile_status">
                            <p>What???</p>
                        </div>
                    </div>
                </div>

                <div id="profile_wrapper_container_FormLogs">
                    <div id="profile_container_status_form">
                        <textarea class="profile_status_form"></textarea>
                    </div>
                    <div id="profile_container_logs">

                        <div class="profile_logs">
                            <p>Added photo on 30th oct</p>
                        </div>
                        <div class="profile_logs">
                            <p>Created a blog <a href="#">'This world'</a> on 17th oct.</p>
                        </div>
                        <div class="profile_logs">
                            <p>Added photo on 16th oct</p>
                        </div>
                        <div class="profile_logs">
                            <p>Updated status 'Do you really think so?' on 10th Oct.</p>
                        </div>
                        <div class="profile_logs">
                            <p>Uploaded video.</p>
                        </div>
                        <div class="profile_logs">
                            <p>Added photo on 30th oct</p>
                        </div>
                        <div class="profile_logs">
                            <p>Created a blog <a href="#">'This world'</a> on 17th oct.</p>
                        </div>
                        <div class="profile_logs">
                            <p>Added photo on 16th oct</p>
                        </div>
                        <div class="profile_logs">
                            <p>Updated status 'Do you really think so?' on 10th Oct.</p>
                        </div>
                        <div class="profile_logs">
                            <p>Uploaded video.</p>
                        </div>
                        <div class="profile_logs">
                            <p>Added photo on 30th oct</p>
                        </div>
                        <div class="profile_logs">
                            <p>Created a blog <a href="#">'This world'</a> on 17th oct.</p>
                        </div>
                        <div class="profile_logs">
                            <p>Added photo on 16th oct</p>
                        </div>
                        <div class="profile_logs">
                            <p>Updated status 'Do you really think so?' on 10th Oct.</p>
                        </div>
                        <div class="profile_logs">
                            <p>Uploaded video.</p>
                        </div>

                    </div>
                </div>

            </div>

另一个html:

    <div id="test">
        <h1>Testing the .load()</h1>
    </div>


    <div id="photo_index">



        <div id="tools_photo">
            <ul id="tools">
                <li><a href="#">All</a></li>
                <li><a href="#">Album</a></li>
                <li><a href="#">Tagged Photo</a></li>
                <li><a href="#">Upload Photo</a></li>
            <span class="clear_left"></span>
            </ul>
        </div>



        <div id="photo_index_page">

                <div id="container_thumbnail_container">
                    <div class="thumbnail_container">
                        <a href="#" class="gallery" title="{{photo.title}}"><img src="img2.jpg" class="thumbnail" /></a>
                    </div>
                    <div class="thumbnail_container">
                        <a href="#" class="gallery" title="{{photo.title}}"><img src="img3.jpg" class="thumbnail" /></a>
                    </div>
                    <div class="thumbnail_container">
                        <a href="#" class="gallery" title="{{photo.title}}"><img src="img4.jpg" class="thumbnail" /></a>
                    </div>
                    <div class="thumbnail_container">
                        <a href="#" class="gallery" title="{{photo.title}}"><img src="img5.jpg" class="thumbnail" /></a>
                    </div>
                    <div class="thumbnail_container">
                        <a href="#" class="gallery" title="{{photo.title}}"><img src="img.jpg" class="thumbnail" /></a>
                    </div>
                    <div class="thumbnail_container">
                        <a href="#" class="gallery" title="{{photo.title}}"><img src="img2.jpg" class="thumbnail" /></a>
                    </div>
                    <div class="thumbnail_container">
                        <a href="#" class="gallery" title="{{photo.title}}"><img src="img3.jpg" class="thumbnail" /></a>
                    </div>
                    <div class="thumbnail_container">
                        <a href="#" class="gallery" title="{{photo.title}}"><img src="img4.jpg" class="thumbnail" /></a>
                    </div>
                    <div class="thumbnail_container">
                        <a href="#" class="gallery" title="{{photo.title}}"><img src="img5.jpg" class="thumbnail" /></a>
                    </div>
                <span class="clear_left"></span>
                </div>
        </div>
    </div>

的.js:

$(document).ready(function () {
    $(".box a").click(function () {
        $("#profile_wrapper_container_FormLogs").load(this.href + "#photo_index");
        return false;
    });
});

3 个答案:

答案 0 :(得分:2)

问题:

您当前的代码会生成http://dummy.com/index.html#photo_index这样的网址,这会从网页加载整个html。

$("#profile_wrapper_container_FormLogs").load(this.href + "#photo_index");

解决方案:

" "

之前加" #photo_index"个空格
$("#profile_wrapper_container_FormLogs").load(this.href + " #photo_index");
                                           //-------------^^--put a space here

或者这样:

$("#profile_wrapper_container_FormLogs").load(this.href + " " +"#photo_index");
                                          //--------------^^--put a space here

答案 1 :(得分:0)

试试这个

var toLoad = this.href+' #photo_index';
$('#profile_wrapper_container_FormLogs').load(toLoad)

答案 2 :(得分:0)

假设另一个html文件中的目标分割元素具有id #target(文件名是another.html) 假设当前html页面中的当前分割元素具有id #result

如果要将目标元素加载到当前结果元素,则

使用此代码 $(&#34;#results&#34;)。load(&#34; another.html #target &#34;,null,function(){ );

第一个参数是文件url,第二个参数是要发送到该url的数据,第三个是回调函数。 #target指定another.html页面中的目标元素。

谢谢你