开发者

.click function in not working on a label

开发者 https://www.devze.com 2023-03-19 00:14 出处:网络
I am having dynamically creating remove label. All have same class but different Ids. I want to get the id when I click a label. I used $(.class).click function but it didn\'t work. I can\'t use oncli

I am having dynamically creating remove label. All have same class but different Ids. I want to get the id when I click a label. I used $(.class).click function but it didn't work. I can't use onclick function becau开发者_Go百科se $(this).attr('id') is not working in IE 8.

Please give me a solution.

Thanks.


.click doesn't work on elements that are created later dynamically. therefore use .live()

$('.class').live('click', function(){ alert(this.id); });

Update:

http://api.jquery.com/live/

As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers.

The above example using .on()

$('body').on('click','.class',function(){ alert(this.id); });


$(".class").click(function(){alert(this.id)});


Try something like this, using the event argument that gets passed to the click handler function instead of this.

$('label.foo').click(function(event) {console.log($(event.target).attr('id'))});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号