2024年3月13日发(作者:)
Javascript获取网页中HTML元素的集中方法分析
getElementById getElementsByName getElementsByTagName 大概介绍
getElementById ,getElementsByName ,getElementsByTagName
后两个是得到集合,byid只是得到单个对象
getElementById 的用法
举个例子:
同一页面内的引用方法:
1、使用id:
,返回值为
2、使用name:
,返回值为
3、使用sourseIndex:
(4).href //注意,前面还有HTML、HEAD、TITLE和BODY,所以是4
4、使用链接集合:
s(0).href
//全部的集合有all、anchors、applets、areas、attributes、behaviorUrns、book
marks、boundElements、cells、childNodes、children、controlRange、elements、
embeds、filters、forms、frames、images、imports、links、mimeTypes、options、
plugins、rows、rules、scripts、styleSheets、tBodies、TextRectangle,请参考MSD
N介绍。
其实方法3和方法4是一样使用的集合,只是一个是all,可以包括页面所有标记,而
anchors只包括链接。
5、getElementById:
mentById("link1").href
6、getElementsByName:
mentsByName("linkname1")[0].href //这也是一个集合,是所有
name等于该方法所带参数的标记的集合
7、getElementsByTagName:


发布评论