$('a').trigger('click'); not working

后端 未结 9 2396
南旧
南旧 2021-02-19 04:06

How do I make jquery click test



        
相关标签:
9条回答
  • 2021-02-19 04:45

    You need to trigger the default click method, not the one by jQuery. This can be done by adding the default click option within a click event of jQuery using this.

    <a href="http://about.com/"></a>
    

    This is how the JavaScript looks. It basically creates the event when the DOM is ready, and clicks it intermediately, thus following the link.

    $(function() {
        $('a').click(function() {
            // 'this' is not a jQuery object, so it will use
            // the default click() function
            this.click();
        }).click();
    });
    

    To see a live example (opening about.com), see: http://jsfiddle.net/8H9UX/

    0 讨论(0)
  • 2021-02-19 04:47
    document.getElementById('mylink').click();
    

    trigger('click') will fire the click event but not the default one.

    $('a').click(function(){ alert('triggered') }) // this will be fired by trigger
    
    0 讨论(0)
  • 2021-02-19 04:49
    $(document).ready(function(){
        $('#mylink').trigger('click');
    });
    
    0 讨论(0)
  • 2021-02-19 04:49

    If you are expecting the file to get downloaded, it will not happen becauer trigger() will not trigger the default event.

    0 讨论(0)
  • 2021-02-19 04:51

    use the following way.... since you want to download the file prevent the link from navigating.

    $(document).ready(function() {
        $('#mylink').click(function(e) {
           e.preventDefault();  //stop the browser navigating
           window.location.href = 'test.zip';
        });
    }); 
    
    0 讨论(0)
  • 2021-02-19 05:01

    You need to wait until the DOM has finished loading. This can be done with jQuery. The anonymous function is run at page load once all the elements are available in the DOM.

    <script>
        $(function() {
            $('#mylink').trigger('click');
        });
    </script>
    
    0 讨论(0)
提交回复
热议问题