jquery template engine, how to bind events
Is there any way to pass function to bind into jQuery template engine开发者_JS百科? http://api.jquery.com/tmpl/
If I create HTML without template I would do it like this:
jQuery("<a>link</a>").click(function(){alert('hi!')}).appendTo( "#forms_container" );
how to do this with jQuery templates?
------------EDIT I found some library here http://knockoutjs.com/documentation/template-binding.html
and seems it something like jQuery.live()
Is there any other ways for binding functions to templates, or this way is used as default mostly?
-----------EDIT Ok, then let it be .delegate. I think for my small task it will be enough, without adding more complexity with Knockout
Similar to Ian's, but a bit different in implementation:
jQuery('#forms_container').delegate('a', 'click', function() {
//Click event, with context (faster)
});
This means that all links in the forms_container will have that click event, no matter what changes. This differs from Ian's (perfectly legitimate) use of live
in that it has a context, so events don't need to bubble all the way up. You can accomplish the same thing using live
(as of 1.4) if you give it context, but I prefer delegate
's syntax.
Give your link something to identify it by (a class or similar or based on the parent container(s)). then use .live
<script id="movieTemplate" type="text/x-jquery-tmpl">
<div id="xyz"><a class="tmplClick">${link}</a> </div>
</script>
then:
$('a.tmplClick').live('click',function(){
alert('clicked in template');
});
精彩评论