使用Golang如何修改HTML内容?
使用Golang如何修改HTML内容? 我一直在寻找一种能够动态修改HTML的解决方案。类似于这个jQuery示例:https://jsfiddle.net/9mo4aezr/2/
使用goquery应该是可行的,但我找不到相应的语法。或者有没有不用goquery就能实现的方法?
我尝试了以下使用goquery的代码,但没有成功:
func addhtml() {
doc, err := goquery.NewDocument("http://127.0.0.1:6060/mainwin")
if err != nil {
log.Fatal(err)
}
doc.Find("#subwin").Add("test")
}
更多关于使用Golang如何修改HTML内容?的实战教程也可以访问 https://www.itying.com/category-94-b0.html
或者不使用 goquery 是否可能?
我现在已经做了一些测试。
- AJAX 只提供数据,不提供 HTML
- 将所有内容存储在表格中只提供数据,不提供 HTML
- iFrame 既提供数据也提供 HTML,并且你可以通过 id 进行简单的数据库查询
http://94.237.92.101:6060/newposts (注意你需要一个更大的屏幕,1800像素以上)
HTML
<tr data-id={{.post_id}}>
<iframe id="subwin" src=""></iframe>
<script>const url = 'c_post?id=';</script>
Javascript
const selected = table.getElementsByClassName('selected')
table.onclick = sel;
function sel(row) {
if (row.target.parentNode.dataset.id > 0) {
if (selected[0]) selected[0].className = '';
row.target.parentNode.className = 'selected';
const el = document.querySelector('selected');
document.getElementById('subwin').src = (url + row.target.parentNode.dataset.id);
}
}
难道真的没有其他方法可以在不使用 iFrame 的情况下,同时用 HTML 和数据填充一个区域吗?
Websockets 可能是一个解决方案,但这似乎有点小题大做。或者呢?
是否可以使用 goquery?
更多关于使用Golang如何修改HTML内容?的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
在Go中修改HTML内容,推荐使用goquery库,它提供了类似jQuery的语法。以下是几种常见操作的示例:
1. 修改元素内容
package main
import (
"fmt"
"github.com/PuerkitoBio/goquery"
"log"
)
func main() {
html := `<div id="container"><p>原始内容</p></div>`
doc, err := goquery.NewDocumentFromReader(strings.NewReader(html))
if err != nil {
log.Fatal(err)
}
// 设置HTML内容
doc.Find("#container").SetHtml("<p>新内容</p>")
// 或者设置文本内容
doc.Find("#container p").SetText("修改后的文本")
// 获取修改后的HTML
htmlStr, _ := doc.Html()
fmt.Println(htmlStr)
}
2. 添加新元素
func addElements() {
html := `<div id="subwin"></div>`
doc, _ := goquery.NewDocumentFromReader(strings.NewReader(html))
// 在#subwin内部添加HTML
doc.Find("#subwin").AppendHtml("<span>追加的内容</span>")
// 在#subwin前面插入
doc.Find("#subwin").BeforeHtml("<div>前面的内容</div>")
// 在#subwin后面插入
doc.Find("#subwin").AfterHtml("<div>后面的内容</div>")
result, _ := doc.Html()
fmt.Println(result)
}
3. 修改属性
func modifyAttributes() {
html := `<a href="#" id="link">链接</a>`
doc, _ := goquery.NewDocumentFromReader(strings.NewReader(html))
// 设置属性
doc.Find("#link").SetAttr("href", "https://example.com")
// 添加类
doc.Find("#link").AddClass("active")
// 移除类
doc.Find("#link").RemoveClass("old-class")
result, _ := doc.Html()
fmt.Println(result)
}
4. 针对你的具体需求
根据你的代码示例,应该是想向#subwin元素添加内容:
func addhtml() {
doc, err := goquery.NewDocument("http://127.0.0.1:6060/mainwin")
if err != nil {
log.Fatal(err)
}
// 正确的方法:向#subwin内部追加HTML
doc.Find("#subwin").AppendHtml("<div>test</div>")
// 或者替换#subwin的内容
doc.Find("#subwin").SetHtml("<div>test</div>")
// 获取修改后的HTML
html, _ := doc.Html()
fmt.Println(html)
}
5. 不使用goquery的方法
如果不想依赖第三方库,可以使用标准库的html/template:
package main
import (
"html/template"
"os"
)
func main() {
tmplStr := `<div id="subwin">{{.Content}}</div>`
tmpl, err := template.New("test").Parse(tmplStr)
if err != nil {
panic(err)
}
data := struct {
Content string
}{
Content: "<p>动态内容</p>",
}
// 渲染模板
err = tmpl.Execute(os.Stdout, data)
if err != nil {
panic(err)
}
}
goquery的Add()方法实际上是用于向选择器添加新元素到文档中,而不是修改现有元素的内容。对于修改操作,应该使用SetHtml()、AppendHtml()、SetText()等方法。

