加载XHTML片段在AJAX使用jQuery [英] Loading XHTML fragments over AJAX with jQuery
问题描述
我试图加载使用jQuery的 $。fn.load
函数XHTML标记的片段,但它提出了一个错误尝试添加新的标记到DOM。我已经缩小下来到XML声明(< XML ...>
?) - 视图工作,如果我回到静态文本,而不申报。我不明白为什么这会导致失败,或者怪在于jQuery的,Firefox或我的code。
我应该如何插入XHTML片段到DOM使用jQuery?
使用$不用彷徨不工作 - 回调收到文件
对象,当我尝试将其插入到DOM中,我收到以下错误:
未捕获的异常:节点不能在层次结构中的特定点插入(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR)
HTTP://本地主机:8000 /静态/ 1222832186 / pixra /脚本/ jQuery的,1.2.6.js
257行
这是我的code:
$身体= $(#DIV来一补);
$获得(/testfile.xhtml,定义,功能(数据)
{
console.debug(数据=%O,数据); //数据=文件
$ body.children().replaceWith(数据); // 错误
},'的xml');
一个示例响应:
< XML版本=1.0编码=UTF-8&GT?;
< DIV的xmlns =http://www.w3.org/1999/xhtml>
<形式的行动=/库/图像/编辑-描述/ 11529 /方法=邮报>
< DIV>内容此处< / DIV>
< /形式GT;
< / DIV>
试试这个,而不是(我只是做了一个快速测试,它似乎工作):
$身体= $(#DIV来一补);
$获得(/testfile.xhtml功能(数据)
{
$ body.html($(数据)。儿童());
},'的xml');
基本上,。儿童()将让你的根节点并替换DIV与它的内容。我想你可以不完全插入带的&lt XML文档; XML声明到DOM ...
I'm trying to load fragments of XHTML markup using jQuery's $.fn.load
function, but it raises an error trying to add the new markup into the DOM. I've narrowed this down to the XML declaration (<?xml...?>
) -- the view works if I return static text without the declaration. I don't understand why this would cause failure, or if the blame lies in jQuery, Firefox, or my code.
How should I insert XHTML fragments into the DOM using jQuery?
Using $.get does not work -- the callback receives a Document
object, and when I try to insert it into the DOM, I receive the following error:
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
This is my code:
$body = $("#div-to-fill");
$.get ("/testfile.xhtml", undefined, function (data)
{
console.debug ("data = %o", data); // data = Document
$body.children ().replaceWith (data); // error
}, 'xml');
A sample response:
<?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>
Try this instead (I just did a quick test and it seems to work):
$body = $("#div-to-fill");
$.get ("/testfile.xhtml", function (data)
{
$body.html($(data).children());
}, 'xml');
Basically, .children() will get you the root node and replace the content of your div with it. I guess you can't exactly insert an xml document with the <?xml declaration into the DOM...
这篇关于加载XHTML片段在AJAX使用jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!