是否有jQuery Click方法? [英] Is there a jQuery Click method?

查看:74
本文介绍了是否有jQuery Click方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jquery单击链接。似乎只有一个复制 onclick(即用户输入)的点击事件。可以使用jquery实际单击链接吗?

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?

推荐答案

从您的答案中:

$("a[0]") 

不是有效的选择器。要获得页面上的第一个a,请使用:

is not a valid selector. to get the first a on the page use:

$("a:first") 

$("a").eq(0). 

因此,答案中的选择器:

So for the selector in your answer:

$("table[1]/tr[1]/td[1]/a").trigger('click'); 

$("table").eq(1).children("tr").eq(1).children('td').eq(1).children('a').click();

请注意,这将如何单击第二个表格行中第二个表格单元格中的所有链接

如果您使用此方法将页面重定向到a的href,则以下方法会稍微好一些:

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:

document.location = $("table").eq(1).children("tr").eq(1).children('td').eq(1).children('a').attr('href');

请注意这将如何将文档位置设置为第二个表格单元格中第一个a的href
如果要匹配第一个元素,请使用eq(0)而不是eq(1)。

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).

编辑

如果您真的想这样做1337-haxxor

EDIT
If you really want to do this 1337-haxxor

$("table:eq(1) > tr:eq(1) > td:eq(1) > a").click();

但是我认为另一种方法更易读。

however I think the other method is more readible.

编辑

好,从您的下一个答案/问题thingie

如何不实际单击链接,但只需为其设置document.location字符串:

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 = $("table").eq(0).children("tr").eq(0).children('td').eq(0).children('a').eq(0).attr('href');

这篇关于是否有jQuery Click方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆