I am trying to get the link that contains a word on a page and refresh them to another page, in a bookmarklet.

我试图在页面上获取包含单词的链接并将其刷新到另一页面。

Here is the code I am not getting to work for a bookmarklet:

这是我没有为书签工作的代码:

   javascript:
        for (i=0; i < document.links.length; i++) {
     if(document.links[i].href.match('exampleword')) {
    location.href = 'http://google.com/exampleword'; 
}
}

Why is this not working?

为什么这不起作用?

2 个解决方案

#1


Greg was sharp to catch that you really want the link "label", rather than the href URL. But I would grab all of the elements with getElementsByTagName('A'). Then iterate through their firstChild nodes.

格雷格很敏锐地发现你真的想要链接“标签”,而不是href网址。但我会用getElementsByTagName('A')获取所有元素。然后遍历他们的firstChild节点。

It's just as easy to pick up the 'nodeValue' of the 'firstChild', as it is to get the innerHTML or (innerText, which someone mentioned, which is less supported).

获取'firstChild'的'nodeValue'同样容易,因为它是获取innerHTML或(innerText,有人提到,支持较少)。

Here's a minimal example, looking for 'word1', 'word2', and 'word3'.

这是一个最小的例子,寻找'word1','word2'和'word3'。

I will just add, that if you want to limit the links looked at for this to a subset of the links on the page or site, you could differentiate in other ways, by setting a particular className to the links you want to search. It would just complicate matters a bit to check the className of the links you grab. Herewith, a simple A Node iteration:

我要补充一点,如果您想将链接的链接限制为页面或网站上的链接子集,则可以通过将特定的className设置为要搜索的链接来区分其他方式。这会使问题复杂化,以检查您抓取的链接的className。这里,一个简单的A Node迭代:

EXAMPLE:

<html>
<head>
<title>JavaScript match link label</title>
<script type="text/javascript">
var keywords = ['word1','word2','word3'];
function linklabels() {
    var aels = document.getElementsByTagName && document.getElementsByTagName('A');
    var aelsCt = aels.length;
    var keywordsCt = keywords.length;
    for (var i = 0; i < aels.length; i++) {
        var v= aels[i].firstChild.nodeValue;
            //this is the link label, the text seen as the link
        for (var j=0; j < keywordsCt; j++) {
            var re = new RegExp(keywords[j]); 
            if (re.test(v)) {
                alert('refreshing to http://google.com/' + v);
                //window.location.href = "http://google.com/" + v;
            }
        }
    }
}
window.onload=linklabels;
</script>
</head>
<body>
<p><a href="#">word1</a> | <a href="#">word2</a> | <a href="#">word3</a>
<br /><a href="#">word1</a> | <a href="#">word2</a> | <a href="#">word3</a></p>
</body>
</html>

更多相关文章

  1. 我可以在JavaScript中从不同的页面传递参数吗?
  2. Ajax的实用技术——用户的注意力从页面的其他区域到转移选择的图
  3. 不断更改页面(例如cookie点击器中的cps)
  4. 切换页面时出现白屏闪烁
  5. 使用CSS,HTML和Javascript在随机图像的页面上进行图像大小调整
  6. Js中获取超链接里面传递的参数值
  7. 如何在涉及css缩放时获取页面上的点击位置
  8. 如何修复一个页面网站的容器/正文宽度(html,css和js)
  9. swiper 定位到指定页面或位置

随机推荐

  1. Android(安卓)studio 入门教程(案例)
  2. Android(安卓)java调用go语言,.go文件生成
  3. android菜单
  4. Android Property System | Android属性
  5. android 条形码的应用
  6. 程序猿214情人节专题----基于GitHub打造
  7. Android布局优化(一)LayoutInflate — 从布
  8. Android基础笔记(一)-快速入门
  9. 让android支持RTSP及live555分析
  10. Android样式开发——layer-list篇