jQuery加载页面并在当前页面中仅添加正文部分 [英] jQuery load a page and add only body part in my current page
问题描述
我正在尝试同时使用$.ajax
或.load()
函数通过ajax加载页面,然后添加其内容来替换当前的$('body').html()
.问题是,当我尝试执行此操作时,jquery将添加所有内容(包括我体内的<head></head>
内容).
I'm trying to use both $.ajax
or .load()
functions to load a page by ajax and than add its content replacing my current $('body').html()
. The problem is than when I try to do it jquery will add everything (included the content of <head></head>
in my body).
$.ajax({
type: 'GET',
url: '/somepage.html',
success: function(data) {
$('body').html(data);
}
});
或
$('body').load('/somepage.html');
结果将是类似的
<body>
<-- Here starts the content of the head -->
<title>....</title>
<script src="..."></script>
<link src="..."></link>
<meta></meta>
<-- Here ends the content of the head -->
<-- Here starts the real body content -->
<div>........</div>
<-- Here ends the body content -->
</body>
如何避免这种情况?我在做错什么吗?
How can I avoid this? Am I doing something wrong?
推荐答案
来自 jQuery load
文档加载页面片段的操作如下:
From the jQuery load
docs loading a page fragment is done as follows:
$('body').load('/somepage.html body');
通过在url
参数中添加第二个选项,您可以控制要加载的内容.您也可以在第二部分中使用css选择器,例如#someId
或.someClass
.
By adding a second option to the url
argument you can control what get loaded. You can also use css selectors in the second part such as #someId
or .someClass
.
这篇关于jQuery加载页面并在当前页面中仅添加正文部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!