简体   繁体   English

从Javascript / JQuery中的超链接文本获取Href

[英]Getting Href From Hyperlink Text in Javascript/JQuery

I am trying to write a Tampermonkey script to help the navigation of some of the websites I commonly browse. 我试图编写一个Tampermonkey脚本来帮助导航我通常浏览的某些网站。 The goal is to be able to browse through the pagination of the page with the arrow keys (for instance, if I am on page 3, left key would go to page 2). 目的是能够使用箭头键浏览页面的分页(例如,如果我在第3页上,则向左键将转到第2页)。 I want to be able to search the page to ensure the Previous link exists, and if it does, click it to go to the previous page. 我希望能够搜索页面以确保存在上一个链接,如果存在,请单击它以转到上一个页面。 An example would be as follows: 一个例子如下:

<a href="www.example.com/page/2.html>Previous</a>

Instead of parsing the url to get the "2" as an integer, incrementing or decrementing as needed, and reconstructing the url, I want to find the word "Previous" and click it if it exists. 我想解析单词“ Previous”并单击它(如果存在),而不是解析该URL以获取整数“ 2”,根据需要递增或递减并重建URL。 How would I go about doing this? 我将如何去做呢? Thank you much for your time! 非常感谢您的宝贵时间!

This is somewhat what I am looking for: http://runnable.com/UhZCuuHhSAsoAALM/how-to-get-a-href-value-using-jquery However, the code uses 这是我在寻找的东西: http : //runnable.com/UhZCuuHhSAsoAALM/how-to-get-a-href-value-using-jquery但是,代码使用

var href = $('a:first').attr('href');

to get the first href on the page. 以获得页面上的第一个href。 I need it to get a specific href on the page (one titled "Previous"). 我需要它来在页面上获得特定的href(标题为“ Previous”)。

Moving backward and forward through the user's history is done using the back(), forward(), and go() methods. 使用back(),forward()和go()方法可以在用户的​​历史记录中前后移动。

window.history.back();
window.history.forward();

Check out MozDev 查看MozDev

<body id="body" data-page='2'>...

javascript javascript

var num = document.getElementById('body').getAttribute('data-page');
document.onkeyup = function(e) {
  if (e.keyCode == 37) {
      window.location.href = "www.example.com/page/"+(num-1)+".html";
  } 
  if (e.keyCode == 39){
      window.location.href = "www.example.com/page/"+(num+1)+".html";
  }
};

// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");
// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";

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

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