最好是在元素生成的时候就加上事件,不然的话比较麻烦。
在jquery中,你也可以用live()和delegate()这样的方法绑定元素,是实时监听的。
但是最好还是在元素生成的时候就加上事件:
$('#btn').bind('click', function(event) {
/* Act on the event */
$("<li>Hello</li>").appendTo("#list").bind('click', function(event) {
/* Act on the event */
console.log($(this).text());
});
});
在mootools中对应的方法:
// Creating an new anchor with an Objectvar myAnchor = new Element('a', {
href: '
http://mootools.net',
'class': 'myClass',
html: 'Click me!',
styles: {
display: 'block',
border: '1px solid black'
},
events: {
click: function(){
alert('clicked');
},
mouseover: function(){
alert('mouseovered');
}
}});