2

我正在尝试使用 ajax 浏览页面。我在这两者之间有一个 id="content" 的页眉和页脚。我想要做的是替换内容,为此我在一个按钮上单击了一个启动以下 JavaScript 方法的按钮:

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

很好,但问题是 home.html 中的东西没有 jQuery css。我试图将它链接到 home.html 文件中。它实际上可以工作,但它会像这样复制页脚:

http://i.stack.imgur.com/x8TZF.png (我不能张贴图片,因为声誉..)

我只是在网上找不到东西,可能是因为很难用几句话来描述这个问题。

我希望有人可以帮助我。:)

谢谢。

4

2 回答 2

0

当使用 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/

于 2013-06-07T09:15:51.977 回答
0

here is a code that works fine. I tested it right now.

//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>

Hope this helps :)

于 2013-06-07T10:14:39.130 回答