最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to emulate click event with jQuery? - Stack Overflow

programmeradmin3浏览0评论

i have simple code:

jQuery(document).ready(function(){
    url = document.location.href.split('#');

    if(url[1] === 'button-show-modal'){
        console.log('true');
        jQuery('#button-show-modal').click();
    }
)

So when page is loaded i see in console true, but no modal appears... If i run jQuery('#button-show-modal').click(); by myself modal appears as it should so generally click event doesn't appear. Where could be the problem?

i have simple code:

jQuery(document).ready(function(){
    url = document.location.href.split('#');

    if(url[1] === 'button-show-modal'){
        console.log('true');
        jQuery('#button-show-modal').click();
    }
)

So when page is loaded i see in console true, but no modal appears... If i run jQuery('#button-show-modal').click(); by myself modal appears as it should so generally click event doesn't appear. Where could be the problem?

Share Improve this question asked May 10, 2013 at 8:40 KinKin 4,59614 gold badges59 silver badges114 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 3

You want to trigger the click in jQuery. The code below is assuming you actually have a click event handler in place already on the element.

jQuery('#button-show-modal').trigger('click');

I did so:

jQuery('#button-show-modal')[0].click();

First index in jQuery object is DOM element: jQuery.get()

发布评论

评论列表(0)

  1. 暂无评论