Javascript getElementsByTagName [英] Javascript getElementsByTagName

查看:104
本文介绍了Javascript getElementsByTagName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用getElementsByTagName(a)方法来获取特定标记下的所有元素。

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

但是,我不希望每一个锚标记。我如何缩小它以便我只选择ul标签下的所有锚标签为someListClass名称?

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

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

I在jQuery中你可以选择$(。someListClass a)。

I know in jQuery you can just select with $(".someListClass a").

我怎么做 没有 使用jQuery?

How would I do it without using jQuery?

推荐答案

给你的ul一个id,然后使用

Give your ul an 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); 




元素是找到的实时NodeList
元素按它们出现在
子树中的顺序。

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

元素是$ b $的元素b搜索应该开始。请注意,此元素的后代只有
是搜索中包含的
,而不是
元素本身。

element is the element from where the search should start. Note that only the descendants of this element are included in the search, but not the element itself.

tagName 是查看
的限定名称。特殊字符串*表示
所有元素。为了与
XHTML兼容,应使用小写。

tagName is the qualified name to look for. The special string "*" represents all elements. For compatibility with XHTML, lower-case should be used.

这篇关于Javascript getElementsByTagName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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