jQuery:在链接上触发()

发布于 2024-08-06 23:23:24 字数 773 浏览 2 评论 0原文

我正在尝试trigger() 一个链接,但它似乎不起作用。

元素具有包含链接 id 的 rel 属性。 如果可能的话,单击该元素应该会触发相应的链接。

我认为问题是我捕获的是 jQuery 单击事件而不是本机链接操作。

无论如何,这里是代码供您查看:

$("#contentmenu li a").click(function(e){
    switch(e.target.id){
        case "opt1":
            alert('do something');
        break;
        case "opt2":
            alert('do something');
        break;
        case "opt3":
            alert('do something');
        break;
    }
    //return false;
});

$("#box_content img").click(function(e){
    menuItem=$(this).attr('rel');
    $('#'+menuItem).trigger('click');
    //return false;
});

编辑

我找到了答案:这样不可能触发链接的本机事件 所以我将改用 window.location

I am trying to trigger() a link but it does not seem to work.

An <img> element has a rel attribute that contains the id of the link.
Clicking on the element should trigger the corresponding link if possible.

I think the problem is that I'm capturing a jQuery click event rather than the native link action.

Anyway, here is the code for you to see:

$("#contentmenu li a").click(function(e){
    switch(e.target.id){
        case "opt1":
            alert('do something');
        break;
        case "opt2":
            alert('do something');
        break;
        case "opt3":
            alert('do something');
        break;
    }
    //return false;
});

$("#box_content img").click(function(e){
    menuItem=$(this).attr('rel');
    $('#'+menuItem).trigger('click');
    //return false;
});

EDIT

I found my answer: It is not possible to trigger the link's native event this way
so I will resort to using window.location instead.

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

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

发布评论

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

评论(1

蓝天 2024-08-13 23:23:24
$('#YourLinkId')[0].click();

我已经在 IE9、IE8 和 Chrome 上进行了测试。
这适用于 IE,但不适用于 Chrome!

$('#YourLinkId')[0].click();

I have Tested on IE9, IE8 and Chrome.
This works on IE , but doesn't work on Chrome !

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