Javascript getElementsByTagName

I am trying to use the getElementsByTagName ("a") method to get all the elements under a specific tag.

However, I do not want every anchor tag. How would I narrow it, so I select all anchor tags under the ul tag for the name "someListClass"?

<ul class="someListClass"> <li><a href... /></li>... </ul>

I know that in jQuery you can just select $ (". SomeListClass a").

How can I do this without using jQuery?

+5
source share
4 answers

Give your ul id and then use

<ul id="ulid" class="someListClass"> <li><a href... /></li>... </ul>

document.getElementById ( "ulid" ).getElementsByTagName ( "a" );

element.getElementsByTagName

elements = element.getElementsByTagName(tagName); 

elements are a live NodeList of the elements found in the order in which they appear the subtree.

Element

- , . , , .

tagName - . "*" . XHTML, .

+12

element.getElementsByTagName(tagName) 

- UL... UL, . - :

<ul class="someListClass" id="myList"> <li><a href... /></li>... </ul>

var theList = document.getElementById('myList');
var listItems = theList.getElementsByTagName('li');
+2

getElementsByClassName http://www.quirksmode.org/blog/archives/2008/05/getelementsbycl.html

  links = document.getElementsByClassName("someListClass")[0].getElementsByTagName("a")
+1

Without a frame, I cannot think of anything else but each element by hand and repeat its parents. If one of them has the class "somelistClass", add it to the stack. Otherwise, no.

If you are looking for children from one element, the Phoenix approach is the way to go.

0
source

All Articles