如何在golang html模板中使用算术运算符

我想将我的 html go Temple 中的字段“FileSize”与我的代码中的变量“minsize”(.FileSize < *minsize)进行比较。我不知道该怎么做。见下文


{{ if lt .FileSize *minsize }}

<td style="color:red;">{{.FileSize}}</td>

{{else}}

<td>{{.FileSize}}</td>

{{end}}


潇湘沐
浏览 334回答 2
2回答

慕婉清6462132

问题出*在您的minsize变量中。如果您尝试取消引用指针,则必须在 go 代码中执行此操作,而不是在模板中。这就是为什么9000有效而*minsize无效的原因。

翻翻过去那场雪

Akama Razor 告诉我,你不需要在这里使用 GO 代码。在这种情况下使用 JS 会好得多。祝你好运!s = document.getElementsByTagName('td');for (i = 0; i < s.length; i++) {&nbsp; if (parseInt(s[i].innerText) > 123) {&nbsp; &nbsp; s[i].style = 'color:red';&nbsp; }}<head>&nbsp; <title>123</title></head><body>&nbsp; <table>&nbsp; &nbsp; <tbody>&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td>123</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>1234</td>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </tbody>&nbsp; </table>&nbsp; <script>&nbsp; &nbsp; s = document.getElementsByTagName('td');&nbsp; &nbsp; for (i = 0; i < s.length; i++) {&nbsp; &nbsp; &nbsp; if (parseInt(s[i].innerText) > 123) {&nbsp; &nbsp; &nbsp; &nbsp; s[i].color = '#fff';&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; </script></body>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go