goquery thehhtml dy not not
我正在尝试使用“ github.com/puerkitobio/goquery”之前插入HTML元素。 则不会添加新元素。
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`)
section.BeforeHtml(`<h1>Team Supreme</h1>`)
goquery.Render(os.Stdout, section)
}
不幸的是,如果我
section.BeforeHtml(`<h1>Team Supreme</h1>`)
不确定
section = section.BeforeHtml(`<h1>Team Supreme</h1>`)
正确的方法是什么是正确的方法,
I am trying to insert an html element before another html element using "github.com/PuerkitoBio/goquery". Unfortunately, the new element is not added ????
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`)
section.BeforeHtml(`<h1>Team Supreme</h1>`)
goquery.Render(os.Stdout, section)
}
The same is true if I replace
section.BeforeHtml(`<h1>Team Supreme</h1>`)
with
section = section.BeforeHtml(`<h1>Team Supreme</h1>`)
Not sure what is the right way of doing it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它不可见,因为您仅渲染选定的部分标签及其内容,但是附加的
h1
元素是在其进行附加h1
标签之前可见的
BeforeHtml
is working as expected it is not visible since you are only rendering selected section tag and its content but the appendedh1
element is before it to make appendedh1
tag visible you have to update below lineTo