繁体   English   中英

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

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

我试图编写一个Tampermonkey脚本来帮助导航我通常浏览的某些网站。 目的是能够使用箭头键浏览页面的分页(例如,如果我在第3页上,则向左键将转到第2页)。 我希望能够搜索页面以确保存在上一个链接,如果存在,请单击它以转到上一个页面。 一个例子如下:

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

我想解析单词“ Previous”并单击它(如果存在),而不是解析该URL以获取整数“ 2”,根据需要递增或递减并重建URL。 我将如何去做呢? 非常感谢您的宝贵时间!

这是我在寻找的东西: http : //runnable.com/UhZCuuHhSAsoAALM/how-to-get-a-href-value-using-jquery但是,代码使用

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

以获得页面上的第一个href。 我需要它来在页面上获得特定的href(标题为“ Previous”)。

使用back(),forward()和go()方法可以在用户的​​历史记录中前后移动。

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

查看MozDev

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

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