jQuery 单击控制另一次单击

发布于 2024-10-24 08:31:47 字数 769 浏览 6 评论 0原文

我想将点击操作从 a#click1 传递到 a#click2。如果我点击 a#click1,则应打开链接 http://www.google.com。怎么做呢?

<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 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

与他有关 2024-10-31 08:31:47

我不明白为什么你所做的不起作用,但如果你想做的只是导航到第二个锚点 href,你可以这样做:

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

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");
    });     
});
故笙诉离歌 2024-10-31 08:31:47

我相信只有当您像为第一个链接那样声明第二个链接的点击功能时,这才有可能。
所以这实际上应该有效:

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

它也应该可以开箱即用!

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!

淡水深流 2024-10-31 08:31:47

这对我有用

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

this works for me

$('a#click1').click(function () {
            $('a#click1').attr('href', $('a#click2').attr("href"));
        }); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文