2013-03-13 35 views
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中的链接。

回答

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') 
相关问题