最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to call a href by onclick event of a button? - Stack Overflow

programmeradmin4浏览0评论

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:

 <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. Basically, I'd like to have the same behaviour of the clicked <a class='openDialog' href when I click a button. How can I do it?**

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:

 <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. Basically, I'd like to have the same behaviour of the clicked <a class='openDialog' href when I click a button. How can I do it?**

Share Improve this question edited Feb 26, 2012 at 14:14 Lightness Races in Orbit 385k77 gold badges665 silver badges1.1k bronze badges asked Feb 25, 2012 at 13:04 LarryLarry 5735 gold badges14 silver badges31 bronze badges 7
  • 1 Where do you assign the handler that opens the dialog (i.e. JavaScript code)? You probably need to alter the selector to include certain buttons. – pimvdb Commented Feb 25, 2012 at 13:06
  • In JQuery, binding to an element's onclick event is as simple as $('.selector').click(function() { ... }). Having said that, I'm not sure what your question is asking... – Karl Barker Commented Feb 25, 2012 at 13:06
  • Thank you all. Sorry, phearps the question is not well formulated...basically, I'd like to have the same behaviour of the <a class='openDialog' href when I click a button. How can I do it? – Larry Commented Feb 25, 2012 at 13:19
  • But what piece of JavaScript code is responsible for opening the dialog? You can probably alter that such that a button does the same thing. – pimvdb Commented Feb 25, 2012 at 13:21
  • Thank you @pimvdb. In fact I need to know how to write the code to reproduce the a href click event... but I have no idea how I can do it. – Larry Commented Feb 25, 2012 at 13:25
 |  Show 2 more ments

2 Answers 2

Reset to default 6

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." 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. 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)

  1. 暂无评论