本文介绍了我的用户脚本如何根据链接的文本获得链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 在目标页面上给定此HTML: < dd class =ddTit> < a href =http://abc.xxx.com/54781.htmltarget =_ blank>特殊文字词< / a> < / dd> 如何获取基于特殊文字的网址,并在Greasemonkey脚本中进行点击? 我尝试过: var textlink = document.querySelector(dd.ddTit a [textContent * ='special']); var clickEvent = document.createEvent('MouseEvents'); clickEvent.initEvent('click',true,true); textlink.dispatchEvent(clickEvent); 没有运气。 我基于 textContent 选择,例如 textContent 包含单词 special p> 解决方案唉,你不能用 querySelector() =http://stackoverflow.com/a/4561376/331508> CSS选择器尚未提供内容/文本选择器。 这里有3种方法: 循环选择一个节点: var ddTitLinks = document.querySelectorAll(dd.ddTit a); for(var J = ddTitLinks.length - 1; J> = 0; --J){ var ddTitLink = ddTitLinks [J] // ---不区分大小写的搜索。 if(/ special text words / i.test(ddTitLink.textContent)){ var clickEvent = document.createEvent('MouseEvents') clickEvent.initEvent('click',true,true); ddTitLink.dispatchEvent(clickEvent); break; } } 使用 jQuery (一个功能强大的库,可以为您节省大量悲伤): // == UserScript == // @name _Click特殊链接 // @include http://YOUR_SERVER.COM/YOUR_PATH/* // @require http://ajax.googleapis.com/ajax/libs/jquery/1.7。 2 / jquery.min.js // == / UserScript == // ---注意,contains()是CASE SENSITIVE。 var specLink = $(dd.ddTit a:contains('special text words')); if(specLink.length){ var clickEvent = document.createEvent('MouseEvents'); clickEvent.initEvent('click',true,true); specLink [0] .dispatchEvent(clickEvent); } 使用XPath: // ---注意,contains区分大小写。 var specLinks = document.evaluate(// dd [contains(@class,'ddTit')] / a [contains(text(),'special text words')], document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null ); if(specLinks.snapshotLength){ var specLink = specLinks.snapshotItem(0); var clickEvent = document.createEvent('MouseEvents'); clickEvent.initEvent('click',true,true); specLink.dispatchEvent(clickEvent); } Given this HTML on the target page:<dd class="ddTit"><a href="http://abc.xxx.com/54781.html" target="_blank">special text words</a></dd>How can I get the url based on the "special text words", and do the click in a Greasemonkey script?I tried this:var textlink = document.querySelector ("dd.ddTit a[textContent*='special']");var clickEvent = document.createEvent ('MouseEvents');clickEvent.initEvent ('click', true, true);textlink.dispatchEvent (clickEvent);with no luck.How do I select based on the textContent, such as textContent contains the word special? 解决方案 Alas, you cannot do it with querySelector() because CSS selectors do not (yet) offer a content/text selector.Here's 3 approaches:Loop through a targeted selection of nodes:var ddTitLinks = document.querySelectorAll ("dd.ddTit a");for (var J = ddTitLinks.length - 1; J >= 0; --J) { var ddTitLink = ddTitLinks[J]; //--- Case-insensitive search. if (/special text words/i.test (ddTitLink.textContent) ) { var clickEvent = document.createEvent ('MouseEvents'); clickEvent.initEvent ('click', true, true); ddTitLink.dispatchEvent (clickEvent); break; }}Use jQuery (a powerful library that will save you a ton of grief):// ==UserScript==// @name _Click Special link(s)// @include http://YOUR_SERVER.COM/YOUR_PATH/*// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js// ==/UserScript==//--- Note that contains() is CASE SENSITIVE.var specLink = $("dd.ddTit a:contains('special text words')");if (specLink.length) { var clickEvent = document.createEvent ('MouseEvents'); clickEvent.initEvent ('click', true, true); specLink[0].dispatchEvent (clickEvent);}Use XPath://--- Note that contains() is CASE SENSITIVE.var specLinks = document.evaluate ( "//dd[contains(@class, 'ddTit')]/a[contains(text(), 'special text words')]", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);if (specLinks.snapshotLength) { var specLink = specLinks.snapshotItem (0); var clickEvent = document.createEvent ('MouseEvents'); clickEvent.initEvent ('click', true, true); specLink.dispatchEvent (clickEvent);} 这篇关于我的用户脚本如何根据链接的文本获得链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-11 11:07