您的位置:首页 > 其它

getElementById与getElementsByName 与getElementsByTagName总结

2009-11-20 21:30 375 查看
getElementById与getElementsByName 与getElementsByTagName总结

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

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

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

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

3.getElementsByTagName (它是获取HTML标签(元素))
作用:按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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐