开发者

jQuery click control another click

开发者 https://www.devze.com 2023-02-19 10:32 出处:网络
I want to pass the click action from a#click1 to a#click2. If I click a#click1, then the link http://www.google.com should be opened. How to do that?

I want to pass the click action from a#click1 to a#click2. If I click a#click1, then the link http://www.google.com should be opened. How to do that?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    jQuery(document).ready(function(){
        $('a#click1').click(function() {
            $('a#click2').click();
        });
    });
</script>
<div id="wrap1">
    <div id="content1">
        <a id="click1" href="">click</a>
    </div>
</div>
<div id="wrap2">
    <div id="content2">
        <a id="click2" href="http://www.google.com">click</开发者_运维问答a>
    </div>
</div>


I can't see why what you have done wouldn't work but if all you want to do is navigate to the 2nd anchors href, you could do:

jQuery(document).ready(function(){
    $('a#click1').click(function() {
        window.location = $('a#click2').attr("href");
    });     
});


I belive this is only possible if you declare a click function for the second link like you did for the first. So this should actually work:

jQuery(document).ready(function() {
$('a#click2').click(function() {
                alert('clicked!');
            });  
            $('a#click1').click(function() {
                $('a#click2').click();
            });     
        });

It should also work for s right out of the box!


this works for me

$('a#click1').click(function () {
            $('a#click1').attr('href', $('a#click2').attr("href"));
        }); 
0

精彩评论

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