在运行时添加的标签的Jquery Click事件不会被触发
我在运行时使用jquery添加标记。
Test
我想触发此标记的click事件,但它不会被触发。
我已经试过了
$('#add').bind('click', function(e) { e.preventDefault(); alert('hello'); });
但没有任何反应。
你需要使用.live()绑定它
$('#add').live('click', function(e) { e.preventDefault(); alert('hello'); });
.live()方法能够通过使用事件委托来影响尚未添加到DOM的元素。
也许你应该使用有效的XHTML:
Test
在jQuery中:
$('#add').live('click', function(e) { e.preventDefault(); alert('hello'); });
在jquery 1.7之后
$( 'document').on( "click",'#step', function() { console.log("Clicking step"); });