1

我正在尝试同时使用$.ajaxor.load()函数来通过 ajax 加载页面,而不是添加其内容来替换我当前的$('body').html(). 问题是当我尝试这样做时,jquery 会添加所有内容(包括<head></head>我正文中的内容)。

$.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>

我怎样才能避免这种情况?难道我做错了什么?

4

1 回答 1

0

jQueryload文档加载页面片段的过程如下:

$('body').load('/somepage.html body');

通过向url参数添加第二个选项,您可以控制加载的内容。您还可以在第二部分中使用 css 选择器,例如#someId.someClass

于 2013-09-17T15:58:58.043 回答