簡體   English   中英

jQuery Ajax-沒有jQuery CSS

[英]jQuery ajax - no jQuery css

我正在嘗試使用Ajax瀏覽頁面。 我有一個頁眉和頁腳的div與這兩個之間的id =“ content”。 我要做的是替換內容,為此,我單擊了啟動以下JavaScript方法的按鈕:

$("#content").load("home.html", function() {
    });

它可以正常工作,但是問題是home.html中的內容沒有jQuery CSS。 我試圖將其鏈接到home.html文件中。 然后它實際上可以工作,但是它像下面這樣復制頁腳:

http://i.stack.imgur.com/x8TZF.png (由於聲譽,我無法發布圖片。)

我只是在網上找不到任何東西,可能是因為很難用幾句話來描述這個問題。

我希望有人能幫助我。 :)

謝謝。

當使用jQuery將另一個HTML頁面的內容加載到當前頁面時,我發現$.load()很難使用,有時也很不靈活。

另一種方法是使用$.get()方法並解析返回的HTML,如下所示:

$.get('home.html', function(response){    
    var myHtml = $('#content', response); //get the section of HTML we want
    $('#content').html(myHtml); //load the returned content into the current page    
});

如果您為選擇器使用相同的ID(我通常使用一個類),那么這將不能很好地工作,但除此之外,它還使您可以更好地控制加載的內容。

希望這可以幫助。

http://api.jquery.com/get/

這是一個工作正常的代碼。 我現在測試了。

//index.html

    <html>
        <head>
            <script src="http://code.jquery.com/jquery-1.9.1.js"></script>

        </head>
        <body>
            <input type="button" id ="click_me" Value="click me to show some content" ></input>
            <div id = "content">

            </div>
        </body>
        <script type="text/javascript">
                $('#click_me').click(function(){

                    $('#content').load("test.html");
                });
        </script>
    </html>

//test.html
<div>

    <p>Loaded content here</p>

</div>

希望這可以幫助 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM