How to call a href by onclick event of a button?

前端 未结 2 716
迷失自我
迷失自我 2021-01-25 04:39

I have the following a href I use to open jquery dialogs, which works fine. Basically, the openDialog class has attached the jquery dialog code:

 &l         


        
相关标签:
2条回答
  • 2021-01-25 04:49

    If I get you question right then may be jQuery trigger()(?) is what you are looking for. Example:

    <button id="bt">Click</button>
    <a href="example.com" id="ex">Triggerable link</a>
    
    <script type="text/javascript">
      $('#bt').click(function() {
           $('#ex').click();
      });
    </script>
    
    0 讨论(0)
  • 2021-01-25 04:55

    You can emulate the click by

     $('#link').click();//No arguments inside the call.
    

    This will emulate the click event on the link. It is the same as clicking on the link. This ofcourse won't change the location if you have an event handler that stops the default behavior of the link If you want to redirect to the href attribute you can use:

     location.href=$('#link').attr('href');
    

    So if you want to call this on click of a button.

    $('#button').click(function(){$('#link').click();
       location.href=$('#link').attr('href');//In case the page doesn't change although you want it to
    }
    
    0 讨论(0)
提交回复
热议问题