Using jQuery to programmatically click an link

后端 未结 9 1895
一生所求
一生所求 2020-11-29 03:55

I know this question has been asked before, but after a search on the web I can\'t seem to find a straight forward answer.

the HTML

         


        
相关标签:
9条回答
  • 2020-11-29 04:15

    I tried few of the above solutions but they didn't worked for me. Here is a link to the page which worked for me automatically click a link

    Above link has many solutions and here's the one which worked for me,

        <button onclick="fun();">Magic button</button>
    
        <!--The link you want to automatically click-->
        <a href='http://www.ercafe.com' id="myAnchor">My website</a>
    

    Now within the <script> tags,

    <script>
    
         function fun(){
              actuateLink(document.getElementById('myAnchor'));
         }
    
         function actuateLink(link)
         {
              var allowDefaultAction = true;
    
              if (link.click)
              {
                  link.click();
                  return;
              }
              else if (document.createEvent)
              {
                  var e = document.createEvent('MouseEvents');
                  e.initEvent(
                       'click'     // event type
                       ,true      // can bubble?
                       ,true      // cancelable?
                  );
                  allowDefaultAction = link.dispatchEvent(e);           
              }
    
              if (allowDefaultAction)       
              {
                  var f = document.createElement('form');
                  f.action = link.href;
                  document.body.appendChild(f);
                  f.submit();
              }
        }
    
    </script>
    

    Copy paste the above code and click on clicking the 'Magic button' button, you will be redirected to ErCafe.com.

    0 讨论(0)
  • 2020-11-29 04:16

    Try this:

    $('#myAnchor')[0].click();
    

    It works for me.

    0 讨论(0)
  • 2020-11-29 04:18

    Click just triggers the click event / events not the actually "goto-the-links-href" action.

    You have to write your own handler and then your $('#myAnchor').trigger('click'); will work...

    $("#myAnchor").click(function(event)
    {
      var link = $(this);
      var target = link.attr("target");
    
      if($.trim(target).length > 0)
      {
        window.open(link.attr("href"), target);
      }
      else
      {
         window.location = link.attr("href");
      }
    
      event.preventDefault();
    });
    
    0 讨论(0)
  • 2020-11-29 04:18
    <a href="#" id="myAnchor">Click me</a>
    
    <script type="text/javascript">
    $(document).ready(function(){
        $('#myAnchor').click(function(){
           window.location.href = 'index.php';
        });
    })
    </script>
    
    0 讨论(0)
  • 2020-11-29 04:23

    Try this for compatibility;

    <script type="text/javascript">
            $(function() {
                setTimeout(function() {
                    window.location.href = $('#myAnchor').attr("href");
    
                }, 1500);
            });
        </script>
    
    0 讨论(0)
  • 2020-11-29 04:26
    window.location = document.getElementById('myAnchor').href
    
    0 讨论(0)
提交回复
热议问题