【问题标题】:Open specific links from a webpage with javascript?使用 javascript 打开网页中的特定链接?
【发布时间】:2014-03-15 01:25:54
【问题描述】:

我正在使用 Tampermonkey,但我似乎无法让 jQuery 处理它,所以这只能是 Javacript。

我正在尝试获取一个脚本来打开(在新窗口中)网页上某个项目的链接。我有一个需要打开的项目列表,以下是这些项目的示例:

<a class="market_listings" href="http://blabla.com/item1">...</a>
<a class="market_listings" href="http://blabla.com/item2">...</a>
<a class="market_listings" href="http://blabla.com/item3">...</a>

   etc.

如您所见,项目由一个类和一个 href 定义。类不是唯一的,但 href 是唯一的。

我是编程新手,但这是我关于如何从页面专门打开这些链接的想法:

  1. 脚本获取具有 class="market_listings" 的元素, 只是为了缩小页面上的 href 搜索范围。

  2. 脚本会查看这些元素的 href 是否与 "http://blabla.com/item*"

  3. 脚本会打开一个带有该 href 的新窗口。

我的编码经验几乎为零,但考虑到我只想打开项目 1 和 3,这就是我的开始方式:

function gethrefandopenwindow() {
         var items = document.getElementsByClassName('market_listings')

//don't know how to code from here

         if (*a href of items corresponds to*: 'http://blabla.com/item1')

            {
             *open new window to href of item1*
             }

         if (*a href of items corresponds to*: 'http://blabla.com/item3')

            {
             *open new window to href of item3*
             }



         else {
              *refresh the page and start over*
              }
}

正如我所说,我几乎没有编程经验,所以我不知道这是否可能,如果可以并且您愿意提供帮助,请像我是一个彻头彻尾的白痴/新手一样向我解释. :)

我能想到的唯一另一种方法是:Javascript getElement by href?;除非我不知道如何在我的情况下应用它(以及如何从元素中打开特定的 href)。

无论如何,我希望你能帮助我,

谢谢!

【问题讨论】:

  • 希望在用户点击链接时打开一个指向其他窗口的 URL?

标签: javascript href tampermonkey getelementsbyclassname


【解决方案1】:

看来您的想法是对的。这个功能可能会让你朝着正确的方向前进。

function OpenHrefsInNewWindow() {
    //Get reference to your elements
    var items = document.getElementsByClassName("market_listings");

    for (var i = 0; i < items.length; i++) //Loop through your elements
    {
        //Verify that the href starts with http://blabla.com/item
        if (items[i].href.indexOf("http://blabla.com/item") === 0)
        {
            //If it does, open that URL in a new window.
            window.open(items[i].href, "_blank");
        }
    }
}

另一种写法:

function OpenHrefsInNewWindow() {
    //Get reference to your elements
    var items = document.getElementsByClassName("market_listings");
    var i = 0;

    while(i < items.length) //Loop through your elements
    {
        //Verify that the href starts with http://blabla.com/item
        if (items[i].href.indexOf("http://blabla.com/item") === 0)
        {
            //If it does, open that URL in a new window.
            window.open(items[i].href, "_blank");
        }

        i++; //Increment i here.
    }
}

【讨论】:

  • 非常感谢,您的回答非常清楚,感谢您抽出宝贵时间帮助我。即使它似乎还没有工作,你肯定帮助我前进!
  • 我很高兴听到它有帮助。您介意将此标记为正确答案吗?
  • 这正是我要做的,但我想先解决我的问题。我把 var i=0 放在上面是因为 tampermonkey 想要这样,但是 tampermonkey 在这里显示一个错误:for (i
  • 谢谢!如果必须在 for 循环范围之外声明变量 (i),则必须移动 i++ 部分,使其成为循环内的最后一行。最后,将 for 更改为 while
  • 修复了错误,谢谢!它似乎没有在网页上完成它的工作,因为它没有打开新窗口,但至少脚本是干净的,谢谢:)
【解决方案2】:

可以获取链接的url取属性值,稍后做ankthing

Url = $(this).attr ('href')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多