【问题标题】:Getting Href From Hyperlink Text in Javascript/JQuery从 Javascript/JQuery 中的超链接文本中获取 Href
【发布时间】:2014-09-19 23:56:02
【问题描述】:

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

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

我不想解析 url 以将“2”作为整数,根据需要递增或递减,并重建 url,我想找到单词“Previous”并单击它(如果存在)。我该怎么做呢?非常感谢您的宝贵时间!

这有点像我想要的: http://runnable.com/UhZCuuHhSAsoAALM/how-to-get-a-href-value-using-jquery 但是,代码使用

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

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

【问题讨论】:

    标签: javascript jquery html redirect hyperlink


    【解决方案1】:

    在用户历史记录中前后移动是使用 back()、forward() 和 go() 方法完成的。

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

    查看MozDev

    【讨论】:

      【解决方案2】:
      <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";
      

      【讨论】:

        猜你喜欢
        • 2015-06-18
        • 1970-01-01
        • 1970-01-01
        • 2014-02-18
        • 1970-01-01
        • 2010-10-17
        • 2013-12-13
        • 1970-01-01
        • 2023-03-14
        相关资源
        最近更新 更多