ITeye博客

getElementById与getElementsByName 与getElementsByTagName总结

java虫 · 2007-10-31 10:44 · JS/DHTML · Markdown

1.getElementById 作用:一般页面里ID是唯一的,用于准备定为一个元素 语法: document.getElementById(id) 参数:id :必选项为字符串(String) 返回值:对象; 返回相同id对象中的第一个,按在页面中出现的次序,如果无符合条件的对象,则返回 null

example:document.getElementById("id1").value;

2.getElementsByName 作用:按元素的名称查找,返回一个同名元素的数组 语法: document.getElementsByName(name) 参数:name :必选项为字符串(String) 返回值:数组对象; 如果无符合条件的对象,则返回空数组,按在页面中出现的次序


example:document.getElementsByName("name1")[0].value;
document.getElementsByName("name1")[1].value;

3.getElementsByTagName 作用:按HTML标签名查询,返回一个相同标签元素的数组 语法: object.getElementsByTagName(tagname) object可以是document或event.srcElement.parentElement等 参数:tagname:必选项为字符串(String),根据HTML标签检索。 返回值:数组对象; 如果无符合条件的对象,则返回空数组,按在页面中出现的次序


example:document.getElementsByTagName("p")[0].childNodes[0].nodeValue;
document.getElementsByTagName("p")[1].childNodes[0].nodeValue

评论(1)

  1. #1 楼 · oliavn · 2008-09-02 13:12

    ** :x

    
    
    
    
    
    

    3.getElementsByTagName 作用:按HTML标签名查 :o :cry: :oops: 8) 询,返回一个相同标签元素的数组 语法: object.getElementsByTagName(tagname) object可以是document或event.srcElement.parentElement等 参数:tagname:必选项为字符串(String),根据HTML标签检索。 返回值:数组对象; 如果无符合条件的对象,则返回空数组,按在页面中出现的次序 :cry: :wink: :cry:

评论功能维护中,当前仅展示历史评论。