通过DOM Traverse自动点击链接,请帮忙

时间:2013-05-24 09:37:57

标签: javascript dom

请参阅以下代码。

我希望能够搜索包含以下代码或非常相似代码的网页。

<table class="fightTable"><tbody><tr><td class="fightClass"><a href="/profile.php?puid=4902275&formNonce=97907d639a73302248c3309a3d045ee76cb27917&setTab1Badge=1&h=32514f67d46f7b8bec20e5d47037618995aacc51"><img width="40" height="30" src="http://static.storm8.com/zl/images/vampire.png?v=330"></img></a></td><td class="fightMobster"><div><a href="/profile.php?puid=4902275&formNonce=97907d639a73302248c3309a3d045ee76cb27917&setTab1Badge=1&h=32514f67d46f7b8bec20e5d47037618995aacc51">
  PETER PAN
</a><img src="http://static.storm8.com/zl/images/indicator_mob.gif?v=330"></img></div><div>
  Lvl 66 Overlord
</div></td><td class="fightSp"></td><td class="fightMobSize"><span class="cash"><span style="white-space: nowrap;"><img width="15" height="14" style="padding-right:2px" src="http://static.storm8.com/zl/images/flesh.png?v=330"></img>
  102
</span></span></td><td class="fightSpLg"></td><td class="fightAction"><script>

    function fsb82308759() {
      var b=new Array(57,97,55,51,51,54,51,100,55,48,48,50,50,57,52,56,55,57,99,51,61,51,101,48,99,110,57,111,78,109,114,97,51,111,102,100,38,57,53,48,52,55,53,101,101,56,48,51,50,55,54,56,61,100,99,73,116,115,98,50,105,55,57,108,116,49,105,104,38,55,116,38,104,104,103,61,105,48,102,51,97,102,53,48,101,61,110,57,111,98,54,56,53,105,116,99,97,38,56,112,54,104,54,112,56,54,46,54,99,116,52,104,52,97,102,103,100,99,50,48,51,105,102,48,56,61,102,98,54,102,97,101,102,101,97,101,100,101,116,63,112,104,112,46,116,115,105,108,116,105,104);
      var p=new Array(0,0,0,1,0,1,0,1,1,0,1,0,0,1,0,0,1,1,0,0,1,0,1,0,1,1,0,1,1,1,1,0,0,1,1,0,1,1,1,0,0,1,0,0,0,1,1,1,1,0,0,1,1,1,0,1,1,1,0,0,1,0,0,1,1,0,1,1,1,0,1,0,1,0,1,0,1,0,0,0,0,1,0,0,0,1,1,0,1,0,0,0,0,1,1,1,1,1,0,1,0,1,0,1,0,0,1,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,1,0,0,1,0,1,0,0,0,0,0,0,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1);       
      window.location = c(b,p);
      return false;
    }

</script><a onclick="return fsb82308759();" href="/hitlist.php?tab=fight.php&action=fight&hitlistId=82308759&formNonce=97907d639a73302248c3309a3d045ee76cb27917&h=284e4fe4946e6fb8af3a662f4583454eebc8bd23"><div class="fightActionInner"><div class="fightActionInnerInner">
  Attack
</div></div></a></td><td class="fightSp"></td></tr></tbody></table>

基本上,我需要创建一个脚本来搜索页面中的某些文本,在这种情况下它将是PETER PAN。

一旦找到,上面的代码中有一个链接,最后标记为“攻击”。我想要创建的脚本,只有找到搜索到的文本时才需要单击此“攻击”链接。

现在,我是这方面的新手。但我认为这可以通过DOM Traversing实现吗?请有人让我知道我该怎么做?或者也许提供一个可以帮助我的脚本片段?非常感谢。 :)

所以我创建了以下内容;

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
var results = [];

var links = document.getElementsByTagName('a');
for(var i = 0; i < links.length; i++)
if(links[i].innerText.trim() === 'PETER PAN')
    results.push(links[i]);
    </script>
</head>

<body>
<a href="http://www.google.com">PETER PAN</a>
</body>
</html>

但实际上并没有做任何事情?它只是加载一个页面,其中包含PETER PAN的链接?如何找到PETER PAN文本,然后在我发布的原始代码中进一步向下单击“Attac”链接?感谢。

1 个答案:

答案 0 :(得分:0)

要查找所有<a>标记,您可以执行以下操作:

var results = [];

var links = document.getElementsByTagName('a');
for(var i = 0; i < links.length; i++)
    if(links[i].innerText.trim() === 'PETER PAN')
        results.push(links[i]);

然后你可以检查结果的长度,以便知道是否找到了文本(如果只需要一个结果,你可以中断循环);

修改 提供攻击链接ID属性,如下所示:

<a id="attack" onclick="return fsb82308759();" href="/hitlist.php?tab=fight.php&action=fight&hitlistId=82308759&formNonce=97907d639a73302248c3309a3d045ee76cb27917&h=284e4fe4946e6fb8af3a662f4583454eebc8bd23"><div class="fightActionInner"><div class="fightActionInnerInner">
Attack
</div></div></a>

然后在js中(在搜索结果的脚本下):

var attackLink = document.getElementById('attack');
if(results.length > 0)
    attackLink.click();