I am adding tag at run time using jquery.
我在运行时使用jquery添加标记。
<a id=add class=add href=#>Test</a>
I want to fire the click event of this tag but it doesn't get fired.
我想触发此标记的click事件,但它不会被触发。
I have already tried
我已经试过了
$('#add').bind('click', function(e) {
e.preventDefault();
alert('hello');
});
but nothing happens.
但没有任何反应。
3 个解决方案
#1
7
You need to bind it using .live()
你需要使用.live()绑定它
$('#add').live('click', function(e) {
e.preventDefault();
alert('hello');
});
The .live() method is able to affect elements that have not yet been added to the DOM through the use of event delegation.
.live()方法能够通过使用事件委托来影响尚未添加到DOM的元素。
http://api.jquery.com/live/
#2
3
Maybe you should use a valid XHTML:
也许你应该使用有效的XHTML:
<a id="add" class="add" href="#">Test</a>
And in the jQuery:
在jQuery中:
$('#add').live('click', function(e) {
e.preventDefault();
alert('hello');
});
#3
2
after jquery 1.7
在jquery 1.7之后
$( 'document').on( "click",'#step', function() {
console.log("Clicking step");
});
#1
7
You need to bind it using .live()
你需要使用.live()绑定它
$('#add').live('click', function(e) {
e.preventDefault();
alert('hello');
});
The .live() method is able to affect elements that have not yet been added to the DOM through the use of event delegation.
.live()方法能够通过使用事件委托来影响尚未添加到DOM的元素。
http://api.jquery.com/live/
#2
3
Maybe you should use a valid XHTML:
也许你应该使用有效的XHTML:
<a id="add" class="add" href="#">Test</a>
And in the jQuery:
在jQuery中:
$('#add').live('click', function(e) {
e.preventDefault();
alert('hello');
});
#3
2
after jquery 1.7
在jquery 1.7之后
$( 'document').on( "click",'#step', function() {
console.log("Clicking step");
});