首先声明的是:

document.getElementByName方法没有。document.getElementsByName得到的是标签的数组
document.getElementId得到的是某一个标签
<form name="form_write">
<input name="content" type="text">

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

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 id='CBylawIndexName' class='normalNode' value='all' >全部</span>"
<span id='CBylawIndexName' class='normalNode' value='ALL' >全部</span>"

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

没有document.getElementByName的更多相关文章

  1. Windows.document

    一.找到元素: document.getElementById("id");根据id找,最多找一个 var a =document.getElementById("id& ...

  2. getElementByName()和getElementById的区别

    因为在属性中,id时唯一的,getElementById取出的是一个元素但是可以出现相同的name,取到的是一个Array ,getElementsByName取出的是数组 记录代码如下: <! ...

  3. js DOM Document类型

    JavaScript通过Document类型访问文档.在浏览器中,document对象是HTMLDocument(继承自 Document类型)的一个实例,表示整个HTML页面.document对象是 ...

  4. 关于document.getElement获取元素返回值的问题

    获取网页元素有很多种方法,如下: document.all[];返回HTMLElement对象 document.all.tags[];返回NodeList对象,类似数组 document.getEl ...

  5. Javascript之document对象用法(很重要)

    一.找到元素 document.getElementById("id"):根据id找层,最多找一个 var a=document.getElementById("id&q ...

  6. JavaScript之document对象使用

    1.document 对象常用的有三种: A.document.getElementById:通过html元素的Id,来获取html对象.适用于单个的html元素. B.document.getEle ...

  7. JavsScript中的Document对象

    Document对象的属性 alinkColor,linkColor,vlinkColor:这些属性描述了超链接的颜色.linkColor指未访问过的链接的正常颜色,vlinkColor指访问过的链接 ...

  8. 从原型链看DOM--Document类型

    JavaScript通过Document类型表示文档,原型链的继承关系为:document.__proto__->HTMLDocument.prototype->Document.prot ...

  9. 批量处理标签属性中document.getElementsByName()的替代方案

    背景 今天在逛知乎时候,看到一个JavaScript方面的问题: 最近在学习JavaScript DOM,就好奇地查阅资料,以及请教学长,得到下面解答: http://www.w3help.org/z ...

随机推荐

  1. PostgreSQL 修改列报错:cannot be cast automatically to type integer

    如果你直接使用可视化工具修改一个varchar字段为int类型的时候,可能会报错, 这里就需要自己去写一个语句去修改了 调整执行语句:ALTER TABLE table_name ALTER COLU ...

  2. 在masterpage中添加对usercontrol的引用

    在masterpage中添加对usercontrol的引用的方式: <%@ Register Src="/_controltemplates/15/Excellent Employee ...

  3. 小技巧之指定refer

    在当前页面A的控制台输入window.location.href='要跳去的页面B',B页面的refer即为A页面.

  4. ubuntu 安装 fcitx

    安装fcitx (1)添加ppa源 sudo add-apt-repository ppa:fcitx-team/nightly 或 sudo add-apt-repository ppa:fcitx ...

  5. 关于C++几个容易混淆的概念总结

    1.函数指针和指针函数 函数指针说的就是一个指针,但这个指针指向的函数,不是普通的基本数据类型或者类对象.函数指针的定义: int (*func)(int a, int b); // 声明函数指针 指 ...

  6. x264_param_t结构

    typedef struct x264_param_t { unsigned int cpu; // CPU 标志位 int i_threads; // 并行编码多帧; 线程数,为0则自动多线程编码 ...

  7. NPOI2.0

    今天在使用NPOI2.0读取上传excel文件(excel2010)时,报了一个很奇怪的错误:无效的 URI: 未能分析主机名. 在网上查找了下找到的出错情况跟这个完全不着边际,没有办法只有自己去测试 ...

  8. dom例子

    //凡是html标签中的属性和值是一样的,那么在js中用true或者false 1,阅读协议倒计时 <input type="button" name="name& ...

  9. 采用python获得并修改文件编码(原创)

    windows和linux采用了不同的编码,这让很多人伤透了脑经,这里我采用了Python的chardet库获得代码的编码,然后修改编码. 1.首先需要安装chardet库,有很多方式,我才用的是比较 ...

  10. PHP 自 5.2 到 5.6 中新增的功能详解

    截至目前(2014.2), PHP 的最新稳定版本是 PHP5.5, 但有差不多一半的用户仍在使用已经不在维护 [注] 的 PHP5.2, 其余的一半用户在使用 PHP5.3 [注].因为 PHP 那 ...