jQuery or JavaScript auto click

后端 未结 6 463
情书的邮戳
情书的邮戳 2020-12-23 21:50

How can I auto click on the link on page load? I have been trying for ages but id does not work.



        
相关标签:
6条回答
  • 2020-12-23 22:26
    $(document).ready(function(){ 
      $('#some-id').trigger('click'); 
    });
    

    did the trick.

    0 讨论(0)
  • 2020-12-23 22:32

    First i tried with this sample code:

    $(document).ready(function(){ 
         $('#upload-file').click(); 
    });
    

    It didn't work for me. Then after, tried with this

    $(document).ready(function(){ 
         $('#upload-file')[0].click(); 
    });
    

    No change. At last, tried with this

    $(document).ready(function(){ 
         $('#upload-file')[0].click(function(){
         }); 
    });
    

    Solved my problem. Helpful for anyone.

    0 讨论(0)
  • 2020-12-23 22:35

    You haven't provided your javascript code, but the usual cause of this type of issue is not waiting till the page is loaded. Remember that most javascript is executed before the DOM is loaded, so code trying to manipulate it won't work.

    To run code after the page has finished loading, use the $(document).ready callback:

    $(document).ready(function(){
      $('#some-id').trigger('click');
    });
    
    0 讨论(0)
  • 2020-12-23 22:38
    $(document).ready(function(){
       $('.lbOn').click();
    });
    

    Suppose this would work too.

    0 讨论(0)
  • 2020-12-23 22:41

    In jQuery you can trigger a click like this:

    $('#foo').trigger('click');
    

    More here:

    http://api.jquery.com/trigger/

    If you want to do the same using prototype, it looks like this:

    $('foo').simulate('click');
    
    0 讨论(0)
  • 2020-12-23 22:47

    You are trying to make a popup work maybe? I don't know how to emulate click, maybe you can try to fire click event somehow, but I don't know if it is possible. More than likely such functionality is not implemented, because of security and privacy concerns.

    You can use div with position:absolute to emulate popup at the same page. If you insist creating another page, I cannot help you. Maybe somebody else with more experience will add his 15 cents.

    0 讨论(0)
提交回复
热议问题