做一些文章詳情頁(yè)的時(shí)候,因?yàn)橛玫搅烁晃谋揪庉嬈鳎虼藬?shù)據(jù)庫(kù)中存儲(chǔ)的是HTML格式的內(nèi)容,當(dāng)從數(shù)據(jù)庫(kù)中讀取顯示到頁(yè)面時(shí)需要進(jìn)行轉(zhuǎn)換,否則顯示就是實(shí)際的字符串內(nèi)容,因此需要進(jìn)行轉(zhuǎn)換,轉(zhuǎn)換的代碼很簡(jiǎn)單,就是通過(guò)關(guān)鍵函數(shù)template.HTML
示例如下:
"content": template.HTML(a.Content),
另外可以自定義模板函數(shù),在模板文件中使用:
package main
import (
"html/template"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
router.SetFuncMap(template.FuncMap{
"safe": func(str string) template.HTML {
return template.HTML(str)
},
})
router.LoadHTMLFiles("./index.tmpl")
router.GET("/index", func(c *gin.Context) {
c.HTML(200, "index.tmpl", "<a href='lizhouwen.com'>1232</a>")
})
router.Run(":9999")
}
html代碼如下:
兒這個(gè)小分享,道哥給大家分享下js中怎么動(dòng)態(tài)輸出一個(gè)表格到頁(yè)面上,這里面呢就涉及到兩個(gè)大的知識(shí)點(diǎn) 一個(gè)是document.wiite一個(gè)是for循環(huán)。請(qǐng)看下例
<script type="text/javascript"> //打印一個(gè)表格 在外層循環(huán)呢先把table標(biāo)簽打印出來(lái) document.write('<table border="1" style="width:500px;">') //下面用for循環(huán)開(kāi)始動(dòng)態(tài)的逐一打印tr和td //第一層for循環(huán)循環(huán)行 tr //打印一個(gè)十行十列的表格 //并且根據(jù)奇偶行和列設(shè)置不同的背景顏色 for(var j=0;j<10;j++) { if(j%2 ==0) { document.write("<tr>") }else { document.write("<tr style='background:green'>") } //內(nèi)層循環(huán)循環(huán)列 td for(var i=0;i<10;i++) { //判斷列的奇偶性 if(i%2==0) { document.write("<td style='background:gray'>"+i+"</td>") }else { document.write("<td>"+i+"</td>") } } document.write("</tr>") } //表格的結(jié)尾 document.write("</table>") </script>
實(shí)現(xiàn)結(jié)果如下
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。