goquery替换html元素用replactwithSelection没有影响
✌️
我试图使用“ github.com/puerkitobio/goquery”替换HTML父元素的子元素。但是,替换为WithSelection不会替换任何东西,并且选择保持不变。
package main
import (
"os"
"strings"
"github.com/PuerkitoBio/goquery"
)
var html = `
<section>
<article>
<h2>Article 1</h2>
<p>Text for article #1</p>
</article>
<article>
<h2>Article 2</h2>
<p>Text for article #2</p>
</article>
</section>
`
func main() {
qHtml, err := goquery.NewDocumentFromReader(strings.NewReader(html))
if err != nil {
panic(err)
}
section := qHtml.Find(`section`)
sectionChildren := section.Children().Clone()
section.ReplaceWithSelection(sectionChildren)
goquery.Render(os.Stdout, section)
}
✌️
I am trying to replace an html parent element with its child elements using "github.com/PuerkitoBio/goquery". However, ReplaceWithSelection does not replace anything and the selection remains unchanged.
package main
import (
"os"
"strings"
"github.com/PuerkitoBio/goquery"
)
var html = `
<section>
<article>
<h2>Article 1</h2>
<p>Text for article #1</p>
</article>
<article>
<h2>Article 2</h2>
<p>Text for article #2</p>
</article>
</section>
`
func main() {
qHtml, err := goquery.NewDocumentFromReader(strings.NewReader(html))
if err != nil {
panic(err)
}
section := qHtml.Find(`section`)
sectionChildren := section.Children().Clone()
section.ReplaceWithSelection(sectionChildren)
goquery.Render(os.Stdout, section)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于
节
仅引用实际文档
中的一个孩子,我们在其中使用replacewithSelection
这些更改将反映在实际文档中,而不是可以从实际文档替换的参考文献,但由于您仍然有参考,因此您将使用实际更改查看它。
要查看更改,而不是使用
节
使用文档
在其中完成了更改。更改
Since
section
just references one of the children in actualDocument
on which we are usingReplaceWithSelection
the changes will be reflected in actual Document instead of reference which may have be replaced from actualDocument
but since you have the reference still you are to view it with the actual changes.To view the changes instead of using
section
useDocument
on which the changes are done.Change
To