javascript 在锚标记中触发 OnClick

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6080173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:22:51  来源:igfitidea点击:

Trigger OnClick in anchor tag

javascriptjquery

提问by GoodSp33d

How can i use trigger() in jquery to emulate onclick attribute of a anchor tag:

我如何在 jquery 中使用 trigger() 来模拟锚标记的 onclick 属性:

<a id="anc" href="#" onclick="someFunction()"> some text </a>

here if i use

在这里,如果我使用

$("anc").trigger('click');

its not working

它不工作

回答by pthurlow

you need to correctly reference the id "anc" in the jQuery selector with a #:

您需要使用 # 正确引用 jQuery 选择器中的 id "anc":

$("#anc").trigger('click');

回答by kobe

you need to use # for id

您需要使用 # 作为 ID

$("#anc").click(function() {
    return false // this will kill the default behaviour and your page won't jump
});

回答by Kanishka Panamaldeniya

when using an id you can use ('#idname').trigger('click'), when using a class you can use ('.classname').trigger)('click')

使用 id 时可以使用('#idname').trigger('click'),使用类时可以使用('.classname').trigger)('click')