SilverlightJavascript loop through childrens of an element

时间:2015-05-24 20:44:36

标签: javascript silverlight-4.0

I have this setup in my xml

  <Grid x:Name="Vakken" Visibility="Collapsed">
     <TextBlock Text="1" Visibility="Collapsed"/>
     <TextBlock Text="2" Visibility="Collapsed"/>
     <TextBlock Text="3" Visibility="Collapsed"/>
     <TextBlock Text="4" Visibility="Collapsed"/>
     <TextBlock Text="5" Visibility="Collapsed"/>
     <TextBlock Text="6" Visibility="Collapsed"/>
     <TextBlock Text="7" Visibility="Collapsed"/>
     <TextBlock Text="8" Visibility="Collapsed"/>
  </Grid>

and what i'm trying to do is loop through all the elements of Grid

classViewPage = document.getElementById('SchermPage');
gridElements = classViewPage.content.findName('Vakken').children;
for (var i = 0; i < gridElements.count; i++) {
        vakkenNamen += gridElements.children[i];
}

i already tried this but it doesn't work. It stops once i try to get an element with [i].

The count itself works

2 个答案:

答案 0 :(得分:0)

If you want to access an array element, you don't need the dot after children:

vakkenNamen += vakkenElementen.children[i];

You also haven't defined vakkenElementen, have you? You should store it in a variable:

var vakkenElementen= classViewPage.content.findName('Vakken');
gridElements = vakkenElementen.children;

After you did this, the code should work as expected.

Edit: Changed vakkenNamen to vakkenElementen according to OP's edit.

Edit: Another edit of the question, considering this, this loop should work:

for (var i = 0; i < gridElements.count; i++) {
        vakkenNamen += gridElements[i];
}

答案 1 :(得分:0)

try this. i think you had a sytax error you didnt need the extra '.' i.e. your code was

vakkenNamen += vakkenElementen.children.[i];

you actually need

vakkenNamen += vakkenElementen.children[i];

full code:

classViewPage = document.getElementById('SchermPage');
gridElements = classViewPage.content.findName('Vakken').children;
for (var i = 0; i < gridElements.count; i++) {
        vakkenNamen += vakkenElementen.children[i];
}

also is vakkenNamen even defined?