开发者

trigger click on element loaded by ajax

开发者 https://www.devze.com 2023-01-31 22:24 出处:网络
how can i trigger click method of an element loaded by ajax runs on click event of a parent element? i 开发者_StackOverflow中文版want it happen of page load time.To trigger an event handler using jQu

how can i trigger click method of an element loaded by ajax runs on click event of a parent element?

i 开发者_StackOverflow中文版want it happen of page load time.


To trigger an event handler using jQuery:

  • .trigger()
  • .triggerHandler()

Could look like:

$(document).ready(function() {
    $('#mybutton').trigger('click');
});

** edit **

After reading your comment, you can use jQuery's .live() or better .delegate() method to have a "live binding" on current and new elements.

$(document).ready(function() {
    $('#mybutton').live('click', function() {
       // do something
    });
});

Ref.: .live(), .delegate()


Try to use .live() method. for more details look at this link.


I had faced same problem in my experience.

My solution is this.

jQuery(document).ready(function($){
$("#element_id").bind('click',function(){
alert('click event is triggered');
});

$("#element_id").trigger('click');
});
0

精彩评论

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