如何像一样触发div

发布于 2024-12-09 05:04:16 字数 1537 浏览 0 评论 0原文

网站上有一个 div 框,并且有一个链接。我想单击容器以触发

<a href="#">sapien sed</a>

但是当我单击容器 div 时,它没有给出响应或者处于无限循环中。 我该如何解决这个问题? 也就是说;我想点击div触发

整个代码:

<div class="container">
    <h2>Lorem ipsum dolor sit amet</h2>
    <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse congue molestie venenatis.
        In adipiscing vulputate sem. Suspendisse potenti. Vestibulum nisl nibh, interdum sit amet pulvinar at,
        malesuada vitae lacus. Aliquam semper <a href="#">sapien sed</a> risus laoreet ultrices. Donec volutpat felis eu justo
        bibendum tempus. Donec non purus sed sapien fringilla tempor ut et metus. Nulla ipsum nibh, dapibus
        ac ornare vitae, fermentum nec nulla.
    </p>
</div>

<script type="text/javascript">
    $(function(){
        $("div.container").each(function(){
            var $container = $(this);
            $container.find("a:first").each(function(){
                var $link = $(this);
                $container
                    .css("cursor", "pointer")
                    .click(function(e){
                        $link.trigger("click");
                        return false;
                    });
                $link.bind("click", function(e){
                    e.stopPropagation();
                    return true;
                });
            });
        });
    })
</script>

There is a div box on the web site and there is a link. I want to click to container in order to trigger <a>:

<a href="#">sapien sed</a>

but When I clicked on the container div, it does not give response or it is in the infinite loop.
How can I solve this problem?
That is to say; I want to click the div to trigger <a>

The whole code:

<div class="container">
    <h2>Lorem ipsum dolor sit amet</h2>
    <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse congue molestie venenatis.
        In adipiscing vulputate sem. Suspendisse potenti. Vestibulum nisl nibh, interdum sit amet pulvinar at,
        malesuada vitae lacus. Aliquam semper <a href="#">sapien sed</a> risus laoreet ultrices. Donec volutpat felis eu justo
        bibendum tempus. Donec non purus sed sapien fringilla tempor ut et metus. Nulla ipsum nibh, dapibus
        ac ornare vitae, fermentum nec nulla.
    </p>
</div>

<script type="text/javascript">
    $(function(){
        $("div.container").each(function(){
            var $container = $(this);
            $container.find("a:first").each(function(){
                var $link = $(this);
                $container
                    .css("cursor", "pointer")
                    .click(function(e){
                        $link.trigger("click");
                        return false;
                    });
                $link.bind("click", function(e){
                    e.stopPropagation();
                    return true;
                });
            });
        });
    })
</script>

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

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

发布评论

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

评论(5

度的依靠╰つ 2024-12-16 05:04:16

问题是您期望 trigger("click") 导致链接的默认行为发生(即您期望链接被跟踪并加载它链接到的页面)。然而,这不是 trigger 所做的。它所做的只是执行绑定到元素的 click 事件处理函数。

要实际点击该链接,您可以获取 href 并使用 window.location

$link.bind("click", function(e){
   window.location = $link.attr("href");
   e.stopPropagation();
   return true;
});

这是一个 更新了小提琴

The problem is that you're expecting trigger("click") to cause the default behaviour of the link to occur (i.e. you are expecting the link to be followed and the page it links to loaded). However, that is not what trigger does. All it does is execute the click event handler function bound to the element.

To actually follow the link, you could grab the href and use window.location:

$link.bind("click", function(e){
   window.location = $link.attr("href");
   e.stopPropagation();
   return true;
});

Here's an updated fiddle.

绻影浮沉 2024-12-16 05:04:16

要同时触发标准“href”链接和带有 onClick 事件的链接:

$(function() { 
    $("div.container").each(function(){
        var $container = $(this);
        $container.find("a:first").each(function(){
            var $this = $(this),
                onClick = this.onclick || null;
            $container
                .css("cursor", "pointer")
                .click(function(e){
                    $link.trigger("click");
                    return false;
                });
            $link.bind("click", function(e){
                if ( !onClick ) {
                    window.location = $(this).attr("href");
                }
                e.stopPropagation();
                return true;
            });
        });
    });
})

To trigger standart "href" links and links with onClick event at the same time:

$(function() { 
    $("div.container").each(function(){
        var $container = $(this);
        $container.find("a:first").each(function(){
            var $this = $(this),
                onClick = this.onclick || null;
            $container
                .css("cursor", "pointer")
                .click(function(e){
                    $link.trigger("click");
                    return false;
                });
            $link.bind("click", function(e){
                if ( !onClick ) {
                    window.location = $(this).attr("href");
                }
                e.stopPropagation();
                return true;
            });
        });
    });
})
蹲墙角沉默 2024-12-16 05:04:16
$(document).ready(function(){
    $('div.container').css("cursor", "pointer").click(function(){
        window.location = $('a', this).first().attr('href');
    });
});
$(document).ready(function(){
    $('div.container').css("cursor", "pointer").click(function(){
        window.location = $('a', this).first().attr('href');
    });
});
笛声青案梦长安 2024-12-16 05:04:16
$("div.container").click(function()
{
  $('a:first', $(this)).click();
});
$("div.container").click(function()
{
  $('a:first', $(this)).click();
});
倚栏听风 2024-12-16 05:04:16

为了确保这一点,请将函数添加到内部,

$(document).ready(function() { });

否则 div 可能无法在代码运行之前加载。

to make sure, add the function inside of the

$(document).ready(function() { });

otherwise the div might not load before the code runs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文