【js常用DOM方法】
介绍几个js DOM的常用方法
获取元素节点 getElementById getElementsByTagName getElementsByClassName
先写一个简单的网页做测试:
/*
test.html
*/
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
</head>
<body>
<p id="contentId" style="width:500px; height: 30px;background-color: #ccc">这段的id是contentId。</p>
<p class="contentClass" style="width:500px; height: 30px;background-color: #ccc">这段的class name是contentClass。</p>
</body>
</html>
1. getElementById
1.先定义变量 var contentId = document.getElementById("contentId");
2.然后输出对象 contentId 就返回id为 contentId 的元素对象( <p id="contentId" style="width:500px; height: 30px;background-color: #ccc"> )。见下图:

2. getElementsByTagName
1.还是先定义变量 var contentTag = document.getElementsByTagName("p");
2.接着我输出 contentTag ,它返回 HTMLCollection [ <p#contentId>, <p.contentClass> ] 共两个,一个以#开头表示id,另一个以.开头表示Class name。
3.继续 contentTag[0] 输出 <p id="contentId" style="width:500px; height: 30px;background-color: #ccc">
contentTag[1] 输出 <p class="contentClass" style="width:500px; height: 30px;background-color: #ccc">

由此可知 getElementsByTagName 返回的是数组!
3. getElementsByClassName
1. var contentClass = document.getElementsByClassName("contentClass");
2. contentClass 输出 HTMLCollection [ <p.contentClass> ] 返回一个元素对象数组,即使只有一个。
3. contentClass[0] 输出 <p class="contentClass" style="width:500px; height: 30px;background-color: #ccc">

我们常用的还有 getAttribute,setAttribute,ChildNodes, nodeType, nodeValue, firstChild, lastChild 方法获取一些信息。
4.分别用 getAttribute 和 setAttribute 获取和设置属性:

改变style属性后:

5.那么这是childNOdes:

也就是说, <p></p> 在遇到块元素时,块元素之间会有一个换行符 <br> ,浏览器在查找子节点时会将它视为一个文本节点。从图中也可以看出 childNodes 返回的也是数组。
那如果是<p#contentId>呢?

nodeType 的值有12种,常用的也就三种:1表示元素节点,2表示属性节点,3表示文本节点。
nodeValue 不仅可以获取文本节点的值,还可以改变文本节点的值。

js的dom方法还有好多,可以看看这个W3school JS参考手册,相信对初学者有很大帮助。
XD!
【js常用DOM方法】的更多相关文章
- JS常用校验方法(判断输入框是否为空,数字,电话,邮件,四舍五入等)
JS常用校验方法: 1.判断输入框是否为空,为空时弹出提示框 2.关闭窗口 3.检查输入字符串是否为数字 4.强制把大写转换成小写 5.手机号码校验,长度为11位数字. 6.电子邮件校验 7.电话号码 ...
- js常用DOM操作
在博客园看到了苏夏写的常用DOM整理文章,地址:http://www.cnblogs.com/cabbagen/p/4579412.html,然后抽时间都试了一下这些常用的DOM操作.在这里记录一下. ...
- JS常用公共方法封装
_ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\||| : |||/ ...
- js常用通用方法
验证身份证详细方法 function isCardNo(pId) { var arrVerifyCode = [1, 0, "x", 9, 8, 7, 6, 5, 4, 3, 2] ...
- JS常用属性方法大全
1. 输出语句 : document.write(""); 2.JS 中的注释为 : // 3. 传统的 HTML 文档顺序是 : document->html->(h ...
- js常用API方法
String对象常用的API:API指应用程序编程接口,实际上就是一些提前预设好的方法. charAt() 方法可返回指定位置的字符. stringObject.charAt(index) index ...
- js常用共同方法
var uh_rdsp = (function(){ //获取根目录 var getContextPath = function(){ var pathName = document.location ...
- 一些JS常用的方法
/** * JS公用类库文件 */ (function(){ Tools = { W: window, D: document, Postfix: ".php", GetId: f ...
- Node.js 常用Mongoose方法
Node.js 手册查询-Mongoose 方法 一.Schema 一种以文件形式存储的数据库模型骨架,无法直接通往数据库端,也就是说它不具备对数据库的操作能力.可以说是数据属性模型(传统意义的表结构 ...
随机推荐
- python要点记录
1.字典:当存储的key数目在几万到几十万之间效率最高.
- L02-RHEL6.5环境中安装JDK1.8
注: 1.本文安装的是jdk1.8,采用rpm包的方式安装. 2.rpm安装方式默认会把jdk安装到/usr/java/jdk1.8xxx 路径上,若想将JDK安装到特定路径,需以源码方式安装,可参考 ...
- TX2 安装v4l
在TX2上使用v4l2-ctl --all -d /dev/video0查看相机参数时报错: v4l2-ctl :command not found 手动安装: sudo apt-get instal ...
- Formatting HDFS
Working on hadoop, especially on test clusters, I have managed to break my HDFS layer and sometimes ...
- c#正则表达式--环视
网上有一篇文章写的很好.请参见 正则表达式之环视 .现将自己的理解记录下来. 环视(lookaround)可以理解为限定条件.即字符串中,某个字符左边或右边能出现什么,不能出现什么.只是一个 ...
- Linus' Law
Given enough eyeballs, all bugs are shallow. ------埃里克 ...
- python实现RSA加密解密方法
python3.5 安装pip 安装rsa python -m pip install rsa 我们可以生成RSA公钥和密钥,也可以load一个.pem文件进来 # -*- coding: utf-8 ...
- mongodb启动失败:child process failed, exited with error number 100
参考 http://www.dataguru.cn/thread-107361-1-1.html 里面的路径 根据自己的--dbpath的路径 和l--logpath路径去找
- Java简单聊天室
实现Java简单的聊天室 所用主要知识:多线程+网络编程 效果如下图 /** * * @author Administrator * * 简单的多人聊天系统——重点:同时性,异步性 * 1.客户端:发 ...
- selector.select()和selector.selectedKeys()
当调用selector.select()时会阻塞: This method performs a blocking selection operation. It returns only after ...