开发者

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

开发者 https://www.devze.com 2023-01-29 22:18 出处:网络
<a href=\"www.google.com\"> <span>Some text</span> <button class=\"triggeranchorclick\"/>
<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

精彩评论

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