[英]How to load inner html of div from another page?
I'm currently working on a website where the navigation bar links would seamlessly lead to another page with fading transitions without loading. 我目前在一个网站上工作,该网站的导航栏链接可以无缝切换到另一个页面,且页面上的淡入淡出效果无需加载。
When the links are clicked, the div#page
block, which has the main page content and the footer, would fade out, empty itself, and fade back in with the content of the href
of the clicked link. 单击链接时,具有主页内容和页脚的
div#page
块将逐渐淡出,清空自身,并随着所单击链接的href
内容逐渐变回。 However, my current code, seen below, placing the html of the entire page (including the head
contents) on div#page
instead of just the inner HTML of div#page
. 但是,我当前的代码,如下图所示,将整个页面的HTML(包括
head
上的内容) div#page
,而不是仅仅的内部HTML的div#page
。
$("#header .menu a, #header .logo").click(function() {
var href = $(this).attr("href");
var title = $(this).attr("title");
history.pushState(null, title, href);
$("#page").fadeOut("slow", function() {
$("#page").delay(1000).empty();
});
$("#page").load(href + "#page", function() {
$("#page").fadeIn();
});
return false;
});
The .load() method, unlike $.get(), allows us to specify a portion of the remote document to be inserted.
与$ .get()不同,.load()方法允许我们指定要插入的远程文档的一部分。 This is achieved with a special syntax for the url parameter.
这可以通过使用url参数的特殊语法来实现。 If one or more space characters are included in the string, the portion of the string following the first space is assumed to be a jQuery selector that determines the content to be loaded.
如果字符串中包含一个或多个空格字符,则假定字符串中第一个空格之后的部分是确定要加载内容的jQuery选择器。
So try adding a white-space like so 因此,尝试像这样添加空白
$("#page").load(href + " #page", function() { //White space before #page
$("#page").fadeIn();
});
PS: I have not tested this code. PS:我尚未测试此代码。
Two options: 两种选择:
Parse the response like so (example): 像这样解析响应(示例):
var $dom = $('<div />'); $dom.html(response); var $content = $dom.find('#id'); // #id is your inner content id
And now you should be able to insert the inner content using $content.html()
. 现在,您应该可以使用
$content.html()
插入内部内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.