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

从Javascript/JQuery中的超链接文本获取Href,javascript,jquery,html,redirect,hyperlink,Javascript,Jquery,Html,Redirect,Hyperlink,我正在尝试编写一个脚本来帮助导航一些我经常浏览的网站。目标是能够使用箭头键浏览页面的分页(例如,如果我在第3页,左键将转到第2页)。我希望能够搜索页面,以确保上一个链接存在,如果存在,请单击它转到上一个页面。 例如: <a href="www.example.com/page/2.html>Previous</a> 获取页面上的第一个href。我需要它在页面上获取特定的href(标题为“Previous”)。通过back()、forward()和go()方法在用户的历史

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

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

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

通过back()、forward()和go()方法在用户的历史记录中前后移动

退房

var href = $('a:first').attr('href');
window.history.back();
window.history.forward();
<body id="body" data-page='2'>...
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";