使用 JavaScript 查找特定链接 [英] Use JavaScript to find a specific link

查看:56
本文介绍了使用 JavaScript 查找特定链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能帮我做一个 JavaScript 函数,它搜索源代码并将网站上的所有链接与我正在寻找的特定链接进行比较.

Could anyone help me with a function for JavaScript which searches the source code and compares all of the links on the site with the specific link I am looking for.

例如:我正在 www.youtube.com 上执行 JavaScript,我正在寻找一个特定的 YouTube 链接.

For example: I am executing the JavaScript on www.youtube.com and I am looking for one specific YouTube link.

它可能看起来像这样(当然这不起作用):

It could look like this (which of course doesn't work):

if(document.body.indexOf("http://www.youtube.com/SPECIFICURL")!== -1){
    console.log("Url found");
}else{
  console.log("Url not found");

}

我怎样才能做到这一点?

How can I accomplish this?

推荐答案

function find_link_by_href(address)
    links = document.getElementsByTagName("a");

    for(var i = 0; i < links.length; i++) { 
      if( links[i].href === address ) {
        console.log("Url found"); 
        return; 
     } 
 }

你可以这样称呼它:

find_link_by_href("http://www.youtube.com/SPECIFICURL");

这篇关于使用 JavaScript 查找特定链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆