我在 golang 模板中有一個動態 id 在范圍內的 html 圖像按鈕。我需要向它添加一個 javascript 函數。但問題是我如何在 javascript 中使用這個動態 Id?我的 HTML{{range $i, $e := .Process}} <input id="id{{.}}" type="image" src="/images/icons/pencil.png" alt="edit" width="15" height="15">{{end}}JavaScript<script type="text/javascript"> $().ready(function() { $('#id{{.}}').click(function() { $('#hidebody').toggle(); }); }); </script> 如何解決這個問題?有沒有更好的方法來做到這一點?
1 回答

12345678_0001
TA貢獻1802條經驗 獲得超5個贊
給這些按鈕一個類。
{{range $i, $e := .Process}}
<input id="{{.}}" class="img-buttons" type="image" src="/images/icons/pencil.png" alt="edit" width="15" height="15">
{{end}}
在javascript中你可以做,
$(".img-buttons").click ( function() {
$(this).attr( "id" ); // get Id
});
如果更適合您,您可以使用 html數據屬性代替 id 。
HTML:
{{range $i, $e := .Process}}
<input data-id="{{.}}" class="img-buttons" type="image" src="/images/icons/pencil.png" alt="edit" width="15" height="15">
{{end}}
JS:
$(".img-buttons").click ( function() {
$(this).data( "id" ); // get Id
});
- 1 回答
- 0 關注
- 145 瀏覽
添加回答
舉報
0/150
提交
取消