gpt4 book ai didi

javascript - replaceWith on XML 问题

转载 作者:数据小太阳 更新时间:2023-10-29 01:52:09 24 4
gpt4 key购买 nike

检查 jQuery 源代码后,我发现我遇到的问题是因为 replaceWith电话 html对于 XML 文档不存在。是replaceWith不应该处理 XML 文档?

我发现了这个公认的简单解决方法,以防将来有人需要它,它将完成我正在尝试做的事情:

xml.find('b').each(function() {
$(this).replaceWith($('<c>yo</c>')) // this way you can custom taylor the XML based on each node's attributes and such
});

但我还是想知道为什么简单的方法行不通。


我不太了解 jQuery,但这不可行吗?

xml = $.parseXML('<a><b>hey</b></a>')
$(xml).find('b').replaceWith('<c>yo</c>')

而不是 xml代表<a><c>yo</c></a>它失败并代表 <a></a> .我做错什么了吗?我正在使用 jQuery 1.6.2。

编辑:

附带说明一下,如果我尝试使用 replaceWith 的函数版本,像这样:

$(xml).find('b').replaceWith(function() {
return '<c>yo</c>' // doesn't matter what I return here
})

我收到这个错误:

TypeError: Cannot call method 'replace' of undefined

编辑 2:

replaceAll但是有效,但我需要使用函数版本,所以我不能满足于此:

$('<c>yo</c>').replaceAll($(xml).find('b')) // works

编辑 3:

这也有效:

xml.find('b').replaceWith($('<c>yo</c>')) // but not with the $() around the argument

最佳答案

这看起来像是 replaceWith() 的设计限制或错误。

当我运行时:

$(xml).find('b').replaceWith(function() {
return '<c>yo</c>';
})

我收到一个 "this[0].innerHTML is undefined" 异常。 See this jsFiddle .

深入 xmlb 节点没有 innerHTML 成员——这有点道理,因为它不是 HTML。 ;)

因此,看起来 replaceWith() 可能并不总是能很好地处理 XML。 Consider reporting a bug .

关于javascript - replaceWith on XML 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6632482/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com