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

Lar*_*rry 1 html javascript jquery onclick button

我有以下a href用于打开jquery对话框,这工作正常.基本上,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>
Run Code Online (Sandbox Code Playgroud)

现在,我想通过onclick一个按钮来调用它. 基本上,<a class='openDialog' href当我点击一个按钮时,我想要点击相同的行为.我该怎么做?**

che*_*123 6

如果我对你提出问题,那么可能是jQuery trigger()(?)就是你要找的.例:

<button id="bt">Click</button>
<a href="example.com" id="ex">Triggerable link</a>

<script type="text/javascript">
  $('#bt').click(function() {
       $('#ex').click();
  });
</script>
Run Code Online (Sandbox Code Playgroud)


Som*_*jee 5

您可以模拟点击次数

 $('#link').click();//No arguments inside the call.
Run Code Online (Sandbox Code Playgroud)

这将模拟链接上的click事件.它与点击链接相同.如果您有一个停止链接默认行为的事件处理程序,则此位置不会更改位置如果要重定向到href属性,可以使用:

 location.href=$('#link').attr('href');
Run Code Online (Sandbox Code Playgroud)

所以,如果你想点击一个按钮来调用它.

$('#button').click(function(){$('#link').click();
   location.href=$('#link').attr('href');//In case the page doesn't change although you want it to
}
Run Code Online (Sandbox Code Playgroud)