是否有 jQuery Click 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/343811/
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
Is there a jQuery Click method?
提问by jedd
I'm trying to click on a link using jquery. There only appears to be a click event that replicates "onclick" (i.e user input). Is it possible to use jquery to actually click a link?
我正在尝试使用 jquery 单击链接。似乎只有一个复制“onclick”(即用户输入)的点击事件。是否可以使用 jquery 来实际单击链接?
回答by Pim Jager
From your answer:
从你的回答:
$("a[0]")
is not a valid selector. to get the first a on the page use:
不是有效的选择器。要获得页面上的第一个 a,请使用:
$("a:first")
or
或者
$("a").eq(0).
So for the selector in your answer:
因此,对于您答案中的选择器:
$("table[1]/tr[1]/td[1]/a").trigger('click');
write
写
$("table").eq(1).children("tr").eq(1).children('td').eq(1).children('a').click();
Note how this will click all the links in the second table cell of the second table row in the second table on your page.
If you use this method to redirect the page to the href of the a the following method is slightly nicer:
请注意这将如何单击页面上第二个表格中第二个表格行的第二个表格单元格中的所有链接。
如果您使用此方法将页面重定向到 a 的 href,则以下方法会稍微好一些:
document.location = $("table").eq(1).children("tr").eq(1).children('td').eq(1).children('a').attr('href');
Note how this will set the document location to the href of the first a found in the second table cell of the second table row found in the second table on the page.
If you want to match the first elements use eq(0) instead of eq(1).
请注意这将如何将文档位置设置为在页面上第二个表格中找到的第二个表格行的第二个表格单元格中找到的第一个 a 的 href。
如果要匹配第一个元素,请使用 eq(0) 而不是 eq(1)。
EDIT
If you really want to do this 1337-haxxor
编辑
如果你真的想做这个 1337-haxxor
$("table:eq(1) > tr:eq(1) > td:eq(1) > a").click();
however I think the other method is more readible.
但是我认为另一种方法更易读。
EDIT
编辑
Okay, from you next answer/question thingie
How about not actually clicking on the link but just setting the document.location string to it:
好的,从你的下一个回答/问题开始,
实际上不点击链接,而是将 document.location 字符串设置为它怎么样:
document.location = $("table").eq(0).children("tr").eq(0).children('td').eq(0).children('a').eq(0).attr('href');
回答by brabster
I prefer $(-some object-).click()for readability
我更喜欢$(-some object-).click()以提高可读性
If you pass the click() method a function, it behaves like an onClick event binding:
如果您向 click() 方法传递一个函数,它的行为就像一个 onClick 事件绑定:
i.e. $(-some object-).click(function() { -do stuff- })
即 $(-some object-).click(function() { -do stuff- })
回答by ElHaix
This works successfully:
这工作成功:
$(document).ready(function() {
$("#horizontalSlideButton").trigger('click');
});
Where horizontalSlideButton is the ID of the link you want to trigger the click event for.
其中horizontalSlideButton 是要为其触发单击事件的链接的ID。
As soon as the DOM is loaded, the contents of $(document).ready(function() {} are loaded.
一旦加载了 DOM,$(document).ready(function() {} 的内容就会被加载。
Please mark if this helps!
如果这有帮助,请标记!
回答by kamal.gs
$(-some object-).trigger('click') should do the trick.
$(-some object-).trigger('click') 应该可以解决问题。
回答by Bojanglez
I had a similar problem and found that by creating a fake event object and then dispatching it using dispatchEvent() replicated a user click very nicely:
我有一个类似的问题,发现通过创建一个假事件对象,然后使用 dispatchEvent() 调度它可以很好地复制用户点击:
var event = document.createEvent("HTMLEvents");
event.initEvent("click", true, true);
document.getElementById('myID').dispatchEvent(event);
Obviously this is not using JQuery, but it might help in your task.
显然这不是使用 JQuery,但它可能对您的任务有所帮助。
回答by bang
$("table:first").find("tr:first").find("td:first").find("a:first")[0].click();
This will work in Internet Explorer if thats your only target, otherwise you're stucked with the document.location solution.
如果那是您唯一的目标,这将在 Internet Explorer 中工作,否则您将被困在 document.location 解决方案中。
回答by Eric
I prefer to use the live function such as (for a class)
我更喜欢使用实时功能,例如(对于一个类)
$(document).ready(function() {
$(".myclass").live('click', function(){//do something});
});
or for an id
或为一个 id
$(document).ready(function() {
$("#myid").live('click', function(){//do something});
});
I use a span class to style my link and give it a class or id like
我使用 span 类来设计我的链接并给它一个类或 id
<span class="link"><a href="javascript:void(0);" target="_new">my link/a></span>
回答by Andreas Grech
Try it this way:
试试这个方法:
$("table:first").find("tr:first").find("td:first").find("a:first").click();
That will trigger the onclick event of the the first a in the first cell of the first row in the first table...and its very readable in itself.
这将触发第一个表格中第一行的第一个单元格中第一个 a 的 onclick 事件......并且它本身非常可读。