简体   繁体   English

如何在Jquery中使用.click()单击超链接?

[英]How to click a hyperlink using .click() in Jquery?

I am trying to read an article but I a am bored of pressing the hyperlink "next page" and tried to run the code below. 我正在尝试阅读一篇文章,但是我对按超级链接“下一页”感到无聊,并试图运行下面的代码。

( what is the code for : Pressing enter will find the hyperlink of class "x-hidden-focus") and click it. 代码是什么 按Enter键将找到类“ x-hidden-focus”的超链接),然后单击它。

The code written below worked by clicking a button when pressing enterKey for another webpage, yet it didn't work with a hyperlink .I tried to run the code that is commented but neither codes fixed my problem. 下面编写的代码可以通过在按下另一个网页的enterKey时单击一个按钮来工作,但是它不适用于超链接。我试图运行带有注释的代码,但是没有一个代码解决了我的问题。

The class of the hyperlink I want to press is ".x-hidden-focus" 我要按的超链接的类别是“ .x-hidden-focus”

This is the link to the article . 这是文章链接

$(document).keypress(function(event){
var which = (event.which ? event.which : event.keyCode);
if(which == '13'){
    //$(".x-hidden-focus")[0].click();
    $(".x-hidden-focus").click();
}
});

NOTE: I am using this code as a userscript in tampermonkey (Hope this helps). 注意:我将此代码用作tampermonkey中的用户脚本(希望有帮助)。

You could try to simply navigate to to the href described by the link you are trying to click: 您可以尝试简单地导航到您要单击的链接所描述的href:

document.location = $("a.x-hidden-focus").attr("href")

Which with your code would become : 与您的代码将成为:

$(document).keypress(function(event){
    var which = (event.which ? event.which : event.keyCode);
    if(which == '13'){
        document.location = $("a.x-hidden-focus").attr("href");
    }
});

Based on the article you have provided we can see that the html for the button you are trying to click is the following: 根据您提供的文章,我们可以看到您要单击的按钮的html如下:

<a href="adding-a-controller" data-linktype="relative-path" class="x-hidden-focus">Next</a>

However if you do press next we can see that there is now 2 buttons : 但是,如果您按下一步,则可以看到现在有两个按钮:

<a href="getting-started" data-linktype="relative-path" class="x-hidden-focus">Previous</a>
<a href="adding-a-view" data-linktype="relative-path" class="x-hidden-focus">Next</a></p>

And now your code would be : 现在您的代码将是:

$(document).keypress(function(event){
    var which = (event.which ? event.which : event.keyCode);
    if(which == '13'){
        document.location = $("a.x-hidden-focus:contains('Next')").attr("href");
    }
});

EDIT 编辑

My assumptions that the class was already present on the element was wrong. 我认为该类已经存在于元素上的假设是错误的。

Since the class is only added after you hover the link you would need to find the link only based on the text: 由于仅在将鼠标悬停在链接之后才添加该类,因此您仅需要根据文本查找链接:

$("a:contains('Next')");

You could however be more precise by using the container class: 但是,通过使用容器类可以更加精确:

$("div.step-by-step").find("a:contains('Next')").attr("href")

The button on the documentation page is dynamically created and the class doesn't exist on it unless you click/hover it. 文档页面上的按钮是动态创建的,除非您单击/将其悬停,否则该类将不存在。 You will need to select the button by 您将需要选择按钮

$('a:contains("Next")')

then get the first one of the resulting three links and take its href 然后获取结果三个链接中的第一个,并获取其href

$('a:contains("Next")').eq(0).attr('href') 

Now you can set the location 现在您可以设置位置

document.location = $('a:contains("Next")').eq(0).attr('href')

$(document).keypress(function(event){
    var which = (event.which ? event.which : event.keyCode);
    if(which == '13'){
        document.location = $('a:contains("Next")').eq(0).attr('href')
    }
});

Just need to add listener for click event like this: 只需为以下单击事件添加侦听器:

$(document).on('keypress', function(e) {
  if (e.keyCode === 13) {
  $('.x-link').click(); 
}
 $('.x-link').on('click', function() {
    let url = $(this).attr('href')
    window.open(url)
  })
})

Here is example 这是例子

As @bambam said You have to select the Link first by: 正如@bambam所说,您必须首先选择链接:

   `$('a:contains("Next")')`

Then navigate to the href described by the link by: 然后通过以下方式导航到链接所描述的href:

    $('a:contains("Next")').eq(0).attr('href')

And you can do the same for the Previous link.Your Final code would be: 您可以对上一个链接进行相同的操作。您的最终代码为:

$(document).keydown(function(event){
var which = (event.which ? event.which : event.keyCode);
if(which == '13'){
    document.location = $('a:contains("Next")').eq(0).attr('href')
}
else if(which == '16'){
    document.location = $('a:contains("Previous")').eq(0).attr('href')
}
});

When you press Enter Keycode:13 you go to the next page. 当您按Enter键 码:13时 ,转到下一页。

When you press Shift Keycode: 16 you go to the previous page. 当您按Shift 键代码时:16转到上一页。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM