开发者

How to make a button click trigger a parent anchor click?

<a href="www.google.com">
  <span>Some text</span>
  <button class="triggeranchorclick"/>
</a>

Using jQuery, how could you cause a click of t开发者_如何学Che button to trigger the link to be clicked - i.e. the user ends up getting redirected to Google.


I don't understand, why do u wanna do this, 'cause you're wrapping the <button> with an <a> tag. so you don't need to write extra bit of code for doing this

Anyway here's my technique,

$(function () {
    $('.triggeranchorclick').bind('click', function (e) {
        window.location.href = $(this).parent('a').attr('href');
    });
});

Test it here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜