如何在JavaScript胡子模板上添加简单的onclick? [英] How do I add a simple onclick on a JavaScript mustache template?

查看:81
本文介绍了如何在JavaScript胡子模板上添加简单的onclick?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的胡子模板:

I have a simple mustache template:

var template = "<button style='background: red;'>{{label}}</button>";

数据:

var data = {

    label: "Click me!"

}

在进行经典转换时,哪个可以很好地配合使用:

Which works great with when doing the classic transformation:

var html = Mustache.to_html( template, data );

但是我的问题.如何在按钮上添加一个简单的onclick函数,该函数是在indata中定义的?

But my question. How do I add a simple onclick function on my button, which I define in the indata?

推荐答案

我认为问题的关键在于,您的页面在知道可能使用或可能不使用的模板之前,不需要对事件进行连线.从外部html文件加载模板,并在模板定义下添加一个带有事件的脚本

I think the point of the question is that your page should not need to wire up events until it knows what template it may or may not be using. Load your template from an external html file and add a script with your events underneath your template definition

这篇关于如何在JavaScript胡子模板上添加简单的onclick?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆