栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

没有document.getElementByName方法

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

没有document.getElementByName方法

首先声明的是:
document.getElementByName方法没有。document.getElementsByName得到的是标签的数组
document.getElementId得到的是某一个标签



然而可以用很浅显的方式得到如:

var fn = document.getElementsByName("form_write")[0]; //得到这个form下的对象
fn.content.value;//就直接去用这个对象取值就可以了。
document.getElementById 1、getElementById

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

example:
复制代码 代码如下:
document.getElementById("id1").value;


2、getElementsByName

作用:按元素的名称查找,返回一个同名元素的数组
语法: document.getElementsByName(name)
参数:name :必选项为字符串(String)
返回值:数组对象; 如果无符合条件的对象,则返回空数组,按在页面中出现的次序
注意:返回数组值为value属性的值,
如果某标签无value属性,当你添加上value属性并赋值后,getElementsByName也能取到其值,
当未对value属性赋值时, getElementsByName返回数组值将是undefined ,
但仍能获得相同name标签的个数document.getElementsByName(name).length
当未设置name属性时document.getElementsByName仍能使用,它将根据你id取得value属性的值

example:
复制代码 代码如下:
document.getElementsByName("name1")[0].value;
document.getElementsByName("name1")[1].value;
全部"
全部"

span标签其实没有name和value属性
但document.getElementsByName("CBylawIndexName")仍将取得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;
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/110475.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号