题
我正在尝试使用jQuery的$.fn.load
函数加载XHTML标记的片段,但是尝试将新标记添加到DOM中会引发错误。我已将其缩小到XML声明(<?xml...?>
) - 如果我在没有声明的情况下返回静态文本,则视图会起作用。我不明白为什么会导致失败,或者责任在于jQuery,Firefox还是我的代码。
我应该如何使用jQuery将XHTML片段插入DOM?
使用$ .get不起作用 - 回调接收到一个Document
对象,当我尝试将其插入DOM时,我收到以下错误:
uncaught exception: Node cannot be inserted at the specified point in the hierarchy (NS_ERROR_DOM_HIERARCHY_REQUEST_ERR)
http://localhost:8000/static/1222832186/pixra/script/jquery-1.2.6.js
Line 257
这是我的代码:
$body = $("#div-to-fill");
$.get ("/testfile.xhtml", undefined, function (data)
{
console.debug ("data = %o", data); // data = Document
$body.children ().replaceWith (data); // error
}, 'xml');
样本回复:
<?xml version="1.0" encoding="UTF-8"?>
<div xmlns="http://www.w3.org/1999/xhtml">
<form action="/gallery/image/edit-description/11529/" method="post">
<div>content here</div>
</form>
</div>
解决方案
尝试这个(我只是做了一个快速测试,它似乎工作):
$body = $("#div-to-fill");
$.get ("/testfile.xhtml", function (data)
{
$body.html($(data).children());
}, 'xml');
基本上,.children()会为您提供根节点并用它替换div的内容。我猜你无法将带有<!> lt;?xml声明的xml文档完全插入DOM ...
不隶属于 StackOverflow