可以在 Underscore 模板的内联 javascript 中使用对象吗?
我正在下划线模板中循环一个数组,以及为每个元素添加 onclick 的内容。我可以以某种方式将对象直接传递给下划线模板中的函数吗?
例如,在这里我试图将客户端元素传递给 editClient() 函数
<table>
<tbody>
<% _.each(clients, function(client) { %>
<tr>
<td><a href="#" onclick="<% editClient(client); %> return false;"><%= client.name %></a></td>
<% }); %>
</tbody>
</table>
我的猜测是下划线只是将所有内容解析为字符串并呈现结果,这意味着我需要将 id 传递给 editClient 函数并获取客户端使用那个。
I am looping an array in an underscore template and what to add onclick for each of the elements. Can I somehow pass the object directly to a function in the underscore template?
e.g. here I'm trying to pass the client element to the editClient() function
<table>
<tbody>
<% _.each(clients, function(client) { %>
<tr>
<td><a href="#" onclick="<% editClient(client); %> return false;"><%= client.name %></a></td>
<% }); %>
</tbody>
</table>
My guess is that underscore just parses everything to a string and renders the result, meaning I would need to instead pass a id to the editClient function and fetch the client using that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于这只是渲染为 html,实际上并没有直接绑定 onclick 事件,因此应该传递 id。
例如
Since this is just rendering to html and not actually directly binding the onclick event the id should be passed instead.
e.g.