我试图更多地理解把手。我有一个simpe图像,我想用手把附加功能 - 但不太了解如何连接它。当我单击此处单击文本时,它可以正常工作,但是当单击img时也希望它工作。
$(function() {
Handlebars.registerHelper('stringifyFunc', function(fn) {
return new Handlebars.SafeString("(" +
fn.toString().replace(/\"/g,"'") + ")()");
});
var source = $('#profile').html();
var template = Handlebars.compile(source);
var data = {
"text": "click here",
img: "#profile",
"func": function(){
alert("Clicked");
}
};
var res = template(data);
$('body').append(template(data));
});
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.rc.1.js"></script>
<script type="text/template" id="profile">
<span onclick="{{stringifyFunc func}}">{{text}}</span>
</script>
<div class='selectorField draggableField'>
<label class="control-label" style="vertical-align:top">Profile:</label>
<div class='selectorField draggableField'>
<img id="profile" src=".png"/>
</div>
</div>