0

我想知道是否可以设计一个脚本来搜索网页中的特定文本字符串,然后直接单击右侧元素 ID 中的链接。

这可能吗。也许是javascript,php?

请提供帮助,并感谢所做的一切。:)

@Four_lo

感谢您的回复。对不起,也许是因为我对 javascript 很陌生,但我无法真正理解您建议的页面上的任何内容。

我整理了一些 javascript,它们将在页面中搜索元素 ID,然后单击其中的链接。

<html>
<head>
<script type="text/javascript">
function init(){
    var linkPage = document.getElementById('linkid').href;
    window.location.href = linkPage;
}
onload=init;
</script>
</head>
<body>

<a href="http://www.barnsley-chronicle.co.uk" id="linkid">GO HERE</a>
<a href="test.html" id="thisone">I WANT TO CLICK HERE!</a>

</body>
</html>

所以基本上,我需要在页面上搜索 GO HERE。然后,一旦找到它,我需要单击 id="thisone" 中的链接,如果这有意义的话。

上面的代码有效,并单击指定 id 内的链接。但是,我想在该 id 中找到某些文本,然后移动到下一个 id,然后单击该 id 中的链接。

4

2 回答 2

0

比它需要的稍微复杂一点:

function performAfterLinkWithText(text, perform) {
    // get all the links
    var $links = document.getElementsByTagName('a');

    // scan them for your text
    for(var i in $links) {
        if($links[i].innerHTML === text) {
            var $next = $links[i] // ready for loop
                , terminateAfter = 20 // don't repeat forever
                ;
            // keep checking the adjacent element
            // because newlines show up as #text
            do {
                $next = $next.nextSibling;
            } while( !$next.href && terminateAfter-- > 0 );

            // do your thing
            perform($next.href, $next); // window.location.href = $next.href;
        }
    }
}

// test -- performAfterLinkWithText('GO HERE', function(url, link) { console.log(url, link); });

performAfterLinkWithText('GO HERE', function(url) { window.location.href = $next.href; });

或者使用 jQuery:

window.location.href = $('a:contains("GO HERE")').next().attr('href')
于 2013-09-10T19:58:38.707 回答
0

有可能的。这可能需要一些技巧,但这里是您应该开始访问所需字符串的地方。我相信正则表达式也是必须的。

http://dom.spec.whatwg.org/#processinginstruction http://domparsing.spec.whatwg.org/

于 2013-03-13T15:11:35.037 回答