开发者

Javascript Elements with class / variable ID

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-VARIAB开发者_如何学PythonLENAMETWO" 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!


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]));
    }
    alert(a[0].id);
    alert(a[1].id);
};

try it out on jsbin


    <body>
    <script type="text/javascript">
        window.onload = function () {
            var outputSpan = document.getElementById('outputSpan'),
                iFrame = frames['subjectIFrame'];

            iFrame.document.location.href = 'http://live.xbox.com/en-US/friendcenter/RecentPlayers?Length=1';
            (function () {
                var nodes = iFrame.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]));
                }
                for (var j in a) if (a.hasOwnProperty(j)) {
                    outputSpan.innerHTML += (a[j] + '<br />');
                }
            })();
        };
    </script>
    <span id="outputSpan"></span>
    <iframe id="subjectIFrame" frameborder="0" height="100" width="100" />
</body>


What does "I can't seem to use document.getElementsByID correctly in this instance" mean? Are you referring to the fact that you are misspelling getElementByID?


So...something like this (jQuery)?

var els = [];

$('.fc-content-panel.fc-friend').each(function() {
  els.push(this));
});

Now you have an array of all the elements that have both of those classes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜