JavaScript元素与类/变量ID [英] Javascript Elements with class / variable ID

查看:257
本文介绍了JavaScript元素与类/变量ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有与一些HTML页面如下:

 < D​​D ID =FC-GTAG-VARIABLENAMEONE级=FC-内容面板FC-朋友>

然后进一步下跌的页面时,code会重复使用,例如:

 < D​​D ID =FC-GTAG-VARIABLENAMETWO级=FC-内容面板FC-朋友>

我如何使用访问外部脚本这些元素?

我似乎无法在这种情况下正确使用的document.getElementById。基本上,我想通过每一行(具体VARIABLENAME(一/二/等)),看起来像上面的两成数组搜索使用世界动物卫生组织(InternetExplorer.Application对象)整个页面使用VBScript创建并拔出。

我已经研究了Javascript和通过试错也没有与此特定页面的任何地方得到,主要是因为没有标签名称和标签ID总是在最后改变。有人能帮忙吗? :)

编辑:我试图使用作为一个答案提供的Javascript来得到结果,但似乎没有任何当应用到我的网页发生。我认为,标签也是一个标记,以便它变得复杂 - 这里是从我会扫描网页上的code的重要组成部分。

 < D​​D ID =FC-GTAG-INDIAN701级=FC-内容面板FC-朋友>
    < D​​IV CLASS =FC-PIC>
        &所述; IMG SRC =htt​​p://image.xboxlive.com/global/t.58570942/tile/0/20400ALT =INDIAN701/>
    < / DIV>
    < D​​IV CLASS =FC-统计>
        < D​​IV CLASS =FC-GTAG>
            <一类=FC-GTAG链接的href =/ EN-US / MyXbox /个人资料的玩家代号= INDIAN701?'> INDIAN701< / A>
            < D​​IV CLASS =FC-gscore图标> 3690< / DIV>
        < / DIV>
        < D​​IV CLASS =FC- presence文本>最近看到10小时前打晕3'; / DIV>
    < / DIV>
    < D​​IV CLASS =FC-行动>
        < D​​IV CLASS =FC-图标行动>
            < D​​IV CLASS =FC-块>
                <跨度类=FC-BUTTONLABEL>阻止用户< / SPAN>
                < A HREF =#类=FC-blockbutton的onclick =返回FriendCenter.BlockFriend('INDIAN701');返回false;>< / A>
            < / DIV>
        < / DIV>
        < D​​IV CLASS =FC-文字行动>
            < D​​IV CLASS =FC-行动>&安培; NBSP;< / DIV>
            <跨度类=FC-行动>
                < A HREF =/ EN-US / MyXbox /个人资料的玩家代号= INDIAN701?>查看个人资料< / A>
            < / SPAN>
            <跨度类=分隔符图标> |< / SPAN>
            <跨度类=FC-行动>
                < A HREF =/ EN-US / GameCenter的的compareTo = INDIAN701?>比较游戏和LT; / A>
            < / SPAN>
            <跨度类=分隔符图标> |< / SPAN>
            <跨度类=FC-行动>
                < A HREF =/ EN-US / MessageCenter /撰写的玩家代号= INDIAN701?>发送短消息< / A>
            < / SPAN>
            <跨度类=分隔符图标> |< / SPAN>
            <跨度类=FC-行动>
                < A HREF =#的onclick =返回FriendCenter.AddFriend('INDIAN701');>发送好友请求< / A>
            < / SPAN>
        < / DIV>
    < / DIV>
< / DD>

这然后重复,用不同的用户名(上面的用户名是INDIAN701)。

我尝试以下,但点击按钮不会产生任何结果:

 < SCRIPT LANGUAGE =VBSCRIPT>
    子window_onLoad      OIE设置= CREATEOBJECT(InternetExplorer.Application)
      oIE.visible = TRUE
      oIE.navigatehttp://live.xbox.com/en-US/friendcenter/RecentPlayers?Length=12    结束小组< / SCRIPT>
<脚本类型=文/ JavaScript的>
    VAR getem =功能(){
      VAR节点= oIE.document.getElementsByTagName(DD),
      一个= [];      为(i的节点){
          (节点[I] .ID)及&放大器; (节点[I] .id.match(/ FC \\ -gtag \\ - /))及&放大器; (a.push(节点[I]));
      }
      警报(一个[0] .ID);
      警报(A [1] .ID);
    }
< / SCRIPT><身体GT;<输入类型=按钮值=尝试的onClick =getem()>
< /身体GT;

基本上我想从最近的球员名单的用户名列表(我希望我不用解释这虽然:))。


解决方案

  VAR getem =功能(){
        VAR节点= document.getElementsByTagName(DD),
            一个= [];        对(在节点变种i)如(节点[I] .ID){
            (节点[I] .id.match(/ FC \\ -gtag \\ - /))及&放大器; (a.push(节点[I] .id.split(' - ')[2]));
        }
        警报(一个[0]);
    };

请尝试点击这里!

There's a page with some HTML as follows:

<dd id="fc-gtag-VARIABLENAMEONE" class="fc-content-panel fc-friend">

Then further down the page, the code will repeat with, for example:

<dd id="fc-gtag-VARIABLENAMETWO" class="fc-content-panel fc-friend">

How do I access these elements using an external script?

I can't seem to use document.getElementByID correctly in this instance. Basically, I want to search the whole page using oIE (InternetExplorer.Application Object) created with VBScript and pull through every line (specifically VARIABLENAME(one/two/etc)) that looks like the above two into an array.

I've researched the Javascript and through trial and error haven't gotten anywhere with this specific page, mainly because there's no tag name, and the tag ID always changes at the end. Can someone help? :)

EDIT: I've attempted to use the Javascript provided as an answer to get results, however nothing seems to happen when applied to my page. I think the tag is ALSO in a tag so it's getting complicated - here's a major part of the code from the webpage I will be scanning.

<dd id="fc-gtag-INDIAN701" class="fc-content-panel fc-friend">
    <div class="fc-pic">
        <img src="http://image.xboxlive.com/global/t.58570942/tile/0/20400" alt="INDIAN701"/>
    </div>
    <div class="fc-stats">
        <div class="fc-gtag">
            <a class="fc-gtag-link" href='/en-US/MyXbox/Profile?gamertag=INDIAN701'>INDIAN701</a>
            <div class="fc-gscore-icon">3690</div>
        </div>
        <div class="fc-presence-text">Last seen 9 hours ago playing Halo 3</div>
    </div>
    <div class="fc-actions">
        <div class="fc-icon-actions">
            <div class="fc-block">
                <span class="fc-buttonlabel">Block User</span>
                <a href="#" class="fc-blockbutton" onclick="return FriendCenter.BlockFriend('INDIAN701'); return false;"></a>
            </div>
        </div>
        <div class="fc-text-actions">
            <div class="fc-action">&nbsp;</div>
            <span class="fc-action">
                <a href="/en-US/MyXbox/Profile?gamertag=INDIAN701">View Profile</a>
            </span>
            <span class="separator-icon">|</span>
            <span class="fc-action">
                <a href="/en-US/GameCenter?compareTo=INDIAN701">Compare Games</a>
            </span>
            <span class="separator-icon">|</span>
            <span class="fc-action">
                <a href="/en-US/MessageCenter/Compose?gamertag=INDIAN701">Send Message</a>
            </span>
            <span class="separator-icon">|</span>
            <span class="fc-action">
                <a href="#" onclick="return FriendCenter.AddFriend('INDIAN701');">Send Friend Request</a>
            </span>
        </div>
    </div>
</dd>

This then REPEATS, with a different username (the above username is INDIAN701).

I tried the following but clicking the button doesn't yield any results:

<script language="vbscript">
    Sub window_onLoad

      Set oIE = CreateObject("InternetExplorer.Application")
      oIE.visible = True
      oIE.navigate "http://live.xbox.com/en-US/friendcenter/RecentPlayers?Length=12"

    End Sub

</script>


<script type="text/javascript"> 
    var getem = function () {
      var nodes = oIE.document.getElementsByTagName('dd'),
      a = [];

      for (i in nodes) {
          (nodes[i].id) && (nodes[i].id.match(/fc\-gtag\-/)) && (a.push(nodes[i]));
      }
      alert(a[0].id);
      alert(a[1].id);
    }
</script> 

<body>

<input type="BUTTON" value="Try" onClick="getem()">


</body>

Basically I'm trying to get a list of usernames from the recent players list (I was hoping I wouldn't have to explain this though :) ).

解决方案

    var getem = function () {
        var nodes = document.getElementsByTagName('dd'),
            a = [];

        for (var i in nodes) if (nodes[i].id) {
            (nodes[i].id.match(/fc\-gtag\-/)) && (a.push(nodes[i].id.split('-')[2]));
        }
        alert(a[0]);
    };

please try it by clicking here!

这篇关于JavaScript元素与类/变量ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆