简体   繁体   English

如何通过按钮的onclick事件调用href?

[英]How to call a href by onclick event of a button?

I have the following a href I use to open jquery dialogs, which works fine. 我使用以下a href来打开jquery对话框,效果很好。 Basically, the openDialog class has attached the jquery dialog code: 基本上,openDialog类已附加了jquery对话框代码:

 <a class='openDialog' data-dialog-id='myEditDlg' data-dialog-autosize='false' data-dialog-alt='580' data-dialog-larg='740' data-dialog-title='test dialog' href='/mycontroller/EditDlg/myid'></a>

Now, I'd like to call it by the onclick event of a button. 现在,我想通过按钮的onclick事件来调用它。 Basically, I'd like to have the same behaviour of the clicked <a class='openDialog' href when I click a button. 基本上,当我单击按钮时,我希望具有与单击的<a class='openDialog' href相同的行为。 How can I do it?** 我该怎么做?**

If I get you question right then may be jQuery trigger() (?) is what you are looking for. 如果我提出正确的问题,那么您可能正在寻找jQuery trigger() (?)。 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>

You can emulate the click by 您可以模仿点击

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

This will emulate the click event on the link. 这将模拟链接上的click事件。 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: 如果您有一个事件处理程序可以停止链接的默认行为,那么该课程当然不会更改位置。如果您想重定向到href属性,则可以使用:

 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM