我如何使用 javascript 获取页面上包含特定单词的链接

发布于 2024-07-22 03:26:40 字数 291 浏览 5 评论 0原文

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

这是我无法用于小书签的代码:

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

为什么这不起作用?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风挽心 2024-07-29 03:26:40

Greg 敏锐地发现您确实需要链接“标签”,而不是 href URL。 但我会使用 getElementsByTagName('A') 获取所有元素。 然后遍历它们的 firstChild 节点。

获取“firstChild”的“nodeValue”和获取“nodeValue”一样​​容易innerHTML 或(innerText,有人提到过,但支持较少)。

这是一个最小的示例,查找“word1”、“word2”和“word3”。

我只想补充一点,如果您想将为此查看的链接限制为页面或网站上链接的子集,您可以通过其他方式进行区分,通过将特定的 className 设置为您想要搜索的链接。 检查您获取的链接的类名只会使事情变得有点复杂。 以下是一个简单的 A 节点迭代:

示例:

<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>

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.

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).

Here's a minimal example, looking for 'word1', 'word2', and '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:

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>
爱的故事 2024-07-29 03:26:40

您正在检查链接 href 是否包含该单词 - 从您的问题来看,这听起来更像您想要锚文本(即您单击的单词)。 最简单的方法(虽然如果 包含其他标签则不是 100% 准确)如下所示:

if (document.links[i].innerHTML.match('exampleword'))

You're checking if the link href contains the word - from your question it sounds more like you want the anchor text (i.e. the words you click on). The easiest way (though not 100% accurate if the <a> contains other tags) is like this:

if (document.links[i].innerHTML.match('exampleword'))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文