如何使用 jQuery 触发对链接的点击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5811122/
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
How to trigger a click on a link using jQuery
提问by Patrioticcow
I have a link:
我有一个链接:
<ul id="titleee" class="gallery">
<li>
<a href="#inline" rel="prettyPhoto">Talent</a>
</li>
</ul>
and I am trying to trigger it by using:
我试图通过使用来触发它:
$(document).ready(function() {
$('#titleee').find('a').trigger('click');
});
But it doesn't work.
但它不起作用。
I've also tried: $('#titleee a').trigger('click');
我也试过: $('#titleee a').trigger('click');
Edit:
编辑:
I actually need to trigger whatever get's called here <a href="#inline" rel="prettyPhoto">
我实际上需要触发这里调用的任何内容 <a href="#inline" rel="prettyPhoto">
回答by Kit Sunde
If you are trying to trigger an event on the anchor, then the code you have will work I recreated your example in jsfiddlewith an added eventHandler so you can see that it works:
如果您试图在锚点上触发事件,那么您拥有的代码将起作用我在jsfiddle 中使用添加的 eventHandler重新创建了您的示例,以便您可以看到它的工作原理:
$(document).on("click", "a", function(){
$(this).text("It works!");
});
$(document).ready(function(){
$("a").trigger("click");
});
Are you trying to cause the user to navigate to a certain point on the webpage by clicking the anchor, or are you trying to trigger events bound to it? Maybe you haven't actually bound the click event successfully to the event?
您是试图通过单击锚点来使用户导航到网页上的某个点,还是试图触发绑定到它的事件?也许您实际上还没有将点击事件成功绑定到事件?
Also this:
还有这个:
$('#titleee').find('a').trigger('click');
is the equivalent of this:
相当于:
$('#titleee a').trigger('click');
No need to call find. :)
无需调用查找。:)
回答by Graham Hotchkiss
Sorry, but the event handler is really not needed. What you do need is another element within the tag to click on.
抱歉,但实际上不需要事件处理程序。您需要的是要单击的标签中的另一个元素。
<a id="test1" href="javascript:alert('test1')">TEST1</a>
<a id="test2" href="javascript:alert('test2')"><span>TEST2</span></a>
Jquery:
查询:
$('#test1').trigger('click'); // Nothing
$('#test2').find('span').trigger('click'); // Works
$('#test2 span').trigger('click'); // Also Works
This is all about what you are clicking and it is not the tag but the thing within it. Unfortunately, bare text does not seem to be recognised by JQuery, but it is by vanilla javascript:
这完全与您单击的内容有关,而不是标签,而是其中的内容。不幸的是,裸文本似乎不被 JQuery 识别,但它是由 vanilla javascript 识别的:
document.getElementById('test1').click(); // Works!
Or by accessing the jQuery object as an array
或者通过访问 jQuery 对象作为数组
$('#test1')[0].click(); // Works too!!!
回答by Alex
Since this question is ranked #1 in Google for "triggering a click on an <a>
element" and no answer actually mentions how you do that, this is how you do it:
由于这个问题在 Google 中因“触发<a>
元素点击”而排名第一,并且实际上没有任何答案提到您如何做到这一点,因此您的做法如下:
$('#titleee a')[0].click();
Explanation: you trigger a click
on the underlying html-element, not the jQuery-object.
说明:您click
在底层 html-element 上触发 a ,而不是 jQuery-object。
You're welcome googlers :)
不客气,谷歌员工:)
回答by Julz
With the code you provided, you cannot expect anything to happen. I second @mashappslabs : first add an event handler :
使用您提供的代码,您不能指望会发生任何事情。我第二次@mashappslabs:首先添加一个事件处理程序:
$("selector").click(function() {
console.log("element was clicked"); // or alert("click");
});
then trigger your event :
然后触发你的事件:
$("selector").click(); //or
$("selector").trigger("click");
and you should see the message in your console.
您应该会在控制台中看到该消息。
回答by Anand Pal
If you are trying to trigger an event on the anchor, then the code you have will work.
如果您尝试在锚点上触发事件,那么您拥有的代码将起作用。
$(document).ready(function() {
$('a#titleee').trigger('click');
});
OR
或者
$(document).ready(function() {
$('#titleee li a[href="#inline"]').click();
});
OR
或者
$(document).ready(function() {
$('ul#titleee li a[href="#inline"]').click();
});
回答by Amin
For links this should work:
对于链接,这应该有效:
eval($(selector).attr('href'));
回答by Harshad Hirapara
This is the demo how to trigger event
这是如何触发事件的演示
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("input").select(function(){
$("input").after(" Text marked!");
});
$("button").click(function(){
$("input").trigger("select");
});
});
</script>
</head>
<body>
<input type="text" value="Hello World"><br><br>
<button>Trigger the select event for the input field</button>
</body>
</html>
回答by Ady Ngom
Well you have to setup the click event first then you can trigger it and see what happens:
那么你必须先设置点击事件,然后你才能触发它,看看会发生什么:
//good habits first let's cache our selector
var $myLink = $('#titleee').find('a');
$myLink.click(function (evt) {
evt.preventDefault();
alert($(this).attr('href'));
});
// now the manual trigger
$myLink.trigger('click');
回答by Matthew Vines
This doesn't exactly answer your question, but will get you the same result with less headache.
这并不能完全回答您的问题,但会以更少的头痛为您提供相同的结果。
I always have my click events call methods that contain all the logic I would like to execute. So that I can just call the method directly if I want to perform the action without an actual click.
我总是让我的点击事件调用方法包含我想要执行的所有逻辑。这样如果我想在没有实际点击的情况下执行操作,我就可以直接调用该方法。
回答by George Filippakis
You should call the element's native .click()
method or use the createEvent
API.
您应该调用元素的本机.click()
方法或使用createEvent
API。
For more info, please visit: https://learn.jquery.com/events/triggering-event-handlers/
更多信息,请访问:https: //learn.jquery.com/events/triggering-event-handlers/