JS—DOM操作
- 节点分为三类:
- 1.元素节点:标签<div></div>
- 2.文本节点:标签内的纯文本。
- 3.属性节点:标签内的属性,id或class
- 查找元素:
- getElementById();参数传递一个元素的id值,这样就可以获取到该元素节点
- DOM操作必须等待HTML文档加载完毕,才可以获取
- 怎样获取?
- 1.把<script>移后
- HTMLDivElement表示Div的节点对象
- IE是以COM实现的DOM,所以只会返回一个object
- 2.用onload事件来加载html
- window.onload = function(){
- var box = document.getElementById('box'); //这里存放当网页所有内容都加载完毕后,再执行的代码
- alert(box);
- }
- 一个页面只能允许一个id,表示唯一性。
- getElementById建议区分大小写,以免不兼容。
- 如果是IE5.0-,不兼容getElementById,可以做个判断
- window.onload = function(){
- if(document.getElementById){
- var box = document.getElementById('box');
- alert(box);
- }else{
- alert('您的浏览器不兼容,请更换~');
- }
- }
- //元素节点的属性
- window.onload = function(){
- var box = document.getElementById('box');
- alert(box.tagName); //获取这个元素节点的标签名
- alert(box.innerHTML); //获取这个元素节点里的文本,纯文本不包含标签
- //innerHTMl获取的是这个元素的文本内容,而不是文本节点。
- }
- window.onload = function(){
- var box = document.getElementById('box');
- box.innerHTML = 'ssssss<strong>ffff</strong>'; //赋值,可以解析HTML,不是单纯的文本。
- }
- //HTML属性的属性
- window.onload = function(){
- var box = document.getElementById('box');
- alert(box.id); //获取这个元素节点id的属性值,注意不是属性节点
- alert(box.title); //获取title属性的值
- alert(box.style); //获取style属性对象
- alert(box.style.color;) //获取style属性对象中color属性的值
- alert(box.className); //获取class属性的值
- }
- //getElementsByTagName()方法将返回一个对象数组HTMLCollection(NodeList),这个数组保存着所有相同元素名的节点列表。
- window.onload = function(){
- var li = document.getElementsByTagName('li'); //参数传递一个标签名即可
- alert(li); //返回一个数组集合,HTMLCollection
- alert(li.length); //返回数组的数量
- alert(li[0]); //HTMLElement,li的节点对象
- alert(li.item(0)); //同上,意义一致。HTMLElement
- alert(li[0].tagName); //LI。获得元素的标签名
- alert(li[0].innerHTML); //
- }
- //返回body节点
- window.onload = function(){
- var body = document.getElementsByTagName('body')[0];
- alert(body); //返回HTMLBodyElement对象,body节点。
- }
- window.onload = function(){
- var all = document.getElementsByTagName('*');
- alert(all.length); //火狐浏览器的firebug打开后,会自动创建一个div,所以会多算一个。
- alert(all[0].tagName); //IE浏览器比火狐和谷歌多一个节点,是把<!文档声明也算进去了>
- }
- window.onload = function(){
- var input = document.getElementsByName('test')[0];
- alert(input.value);
- alert(input.checked);
- };
- //getAttribute() 方法返回指定属性名的属性值。
- window.onload = function(){
- var box = document.getElementById('box');
- alert(box.getAttribute('style')); //非IE返回的是style字符串,IE返回的是对象,这里有个不兼容
- alert(box.getAttribute('bbb')); //自定义可以兼容
- alert(box.getAttribute('class')); //IE无法获取
- alert(box.getAttribute('className')); //IE可以获取,非IE无法获取
- //跨浏览器获取className
- if(box.getAttribute('className')==null){
- alert(box.getAttribute('class'));
- }else{
- alert(box.getAttribute('className'));
- }
- };
- window.onload = function(){
- var box = document.getElementById('box');
- alert(box.onclick); //均返回函数式;
- alert(box.getAttribute('onclick')); //IE7以下会返回函数式,非IE返回字符串
- };
- window.onload = function(){
- var box = document.getElementById('box');
- //box.setAttribute('title','abc'); //创建一个属性和属性值
- //box.setAttribute('align','center');
- //box.setAttribute('bbb','cccc');
- box.setAttribute('style','color:green'); //IE7以下,style和onclick没有效果,避免使用
- };
- window.onload = function(){
- var box = document.getElementById('box');
- box.removeAttribute('style'); //IE6一下不支持removeAttribute
- };
- 3.DOM节点
- window.onload = function(){
- var box = document.getElementById('box');
- alert(box.nodeName); //获取元素节点的标签名,和tagName等价
- alert(box.nodeType); //获取元素节点的类型值,1
- alert(box.nodeValue); //元素节点本身没有内容,所以是null
- //node本身把节点指针放在元素<div>上,也就是说,本身是没有value
- //如果要输出<div>中里面包含的文本内容,那么前面innerHTML
- alert(box.innerHTML); //获取元素节点里面的文本内容
- //node只能获取当前节点的东西。
- //文本节点 不等于 文本内容
- };
- window.onload = function(){
- var box = document.getElementById('box');
- alert(box.childNodes); //NodeList集合,返回当前元素节点所有的子节点列表
- alert(box.childNodes.length); //3个子节点
- //3个子节点为:div<em>sss</em>fefef
- //第一个子节点为:div,这个节点称作为:文本节点
- //第二个子节点为:<em>sss</em>,这个节点称作为:元素节点
- //第三个子节点为:文本节点。
- alert(box.childNodes[0]); //Object Text表示一个文本节点对象
- alert(box.childNodes[0].nodeType); //3,表示文本节点
- alert(box.childNodes[0].nodeValue); //获取文本节点的文本内容
- alert(box.childNodes[0].innerHTML); //当前的文本,找不到里面的内容,undefined
- alert(box.childNodes[0].nodeName); //#text,文本节点没有标签名
- };
- //通过判断节点类型,来获取不同的输出
- window.onload = function(){
- var box = document.getElementById('box');
- for(var i = 0; i<box.childNodes.length; i++){
- if(box.childNodes[i].nodeType === 1){
- alert('元素节点:'+box.childNodes[i].nodeName);
- }else if(box.childNodes[0].nodeType === 3){
- alert('文本节点:'+box.childNodes[i].nodeValue);
- }
- }
- };
- window.onload = function(){
- var pox = document.getElementById('pox');
- //pox.innerHTML = 'sssss';
- //pox.nodeValue = 'eefefeffef'; //没出错,但没有赋值上,nodeValue必须在当前节点上操作。
- pox.childNodes[0].nodeValue = 'dfdfdff';
- };
- window.onload = function(){
- var box = document.getElementById('box');
- //alert(box.childNodes[0].nodeValue); //获取第一个子节点
- //alert(box.childNodes[box.childNodes.length -1].nodeValue); //获取最后一个子节点
- };
- firstChild和lastChild属性
- window.onload = function(){
- var box = document.getElementById('box');
- alert(box.firstChild.nodeValue); //获取第一个子节点
- alert(box.lastChild.nodeValue); //获取最后一个子节点
- };
- //返回文档对象的根节点
- window.onload = function(){
- var box = document.getElementById('box');
- //alert(box.ownerDocument); //HTMLDocument
- //alert(document); //HTMLDocument
- alert(box.ownerDocument === document);
- };
- window.onload = function(){
- var box = document.getElementById('box');
- //alert(box.parentNode); //HTMLBodyElement
- //alert(box.firstChild.nextSibling); //HTMLSpanElement
- //alert(box.firstChild.nextSibling.nodeName); //下一个同级节点的标签名
- //alert(box.lastChild.previousSibling.nodeName); //上一个同级节点的标签名
- };
- //attributes 属性返回指定节点的属性集合,即 NamedNodeMap。
- window.onload = function(){
- var box = document.getElementById('box');
- //alert(box.attributes); //NamedNodeMap,集合数组,保存着这个元素节点的属性列表
- //alert(box.attributes.length);
- //alert(box.attributes[0]); //Attr,属性节点
- //alert(box.attributes[0].nodeType); //2,属性节点的类型值
- //alert(box.attributes[0].nodeValue); //box,第一个属性的属性值
- //lert(box.attributes[0].nodeName); //id,第一个属性的属性名
- //alert(box.attributes['title'].nodeValue); //遍历的时候,是从后向前的
- };
- window.onload = function(){
- var box = document.getElementById('box');
- alert(box.childNodes.length); //有7个节点,包括了空白节点
- };
- window.onload = function(){
- var box = document.getElementById('box');
- alert(filterWhiteNode(box.childNodes).length);
- };
- //忽略空白字符
- function filterWhiteNode(node){
- var ret=[];
- for(var i = 0; i<node.length; i++){
- if(node[i].nodeType === 3 && /^\s+$/.test(node[i].nodeValue)){
- continue;
- }else{
- ret.push(node[i]);
- }
- }
- return ret;
- }
- //移除空白字符
- function filterWhiteNode(node){
- for(var i = 0; i<node.childNodes.length; i++){
- if(node.childNodes[i].nodeType === 3 && /^\s+$/.test(node.childNodes[i].nodeValue)){
- node.childNodes[i].parentNode.removeChild(node.childNodes[i]);
- }
- }
- return node;
- }
- window.onload = function(){
- var box = document.getElementById('box');
- var p = document.createElement('p'); //只是创建了一个元素节点p,还没有添加到文档中去
- box.appendChild(p); //将新节点p添加到id=box的div里的子节点列表的末尾上
- var text = document.createTextNode('sdfsdfsdf'); //创建一个文本节点
- p.appendChild(text); //把文本添加到p里面
- box.appendChild(p);
- document.getElementsByTagName('body')[0].appendChild(p);
- };
- window.onload = function(){
- var box = document.getElementById('box');
- var p = document.createElement('p');
- //box.parentNode就是body
- box.parentNode.insertBefore(p,box); //在box的父节点添加一个p,就是在box前面添加一个元素节点
- };
- //向后添加节点
- window.onload = function(){
- var box = document.getElementById('box');
- var p = document.createElement('p');
- insertAfter(p,box);
- };
- function insertAfter(newElement,targetElement){
- //得到父节点,就是body,但是不能直接使用body,如果层次较多,父节点不一定是body
- var parent = targetElement.parentNode;
- if(parent.lastChild === targetElement){
- alert('ss');
- parent.appendChild(newElement,targetElement)
- }else{
- //span节点的前面添加,可以用insertBefore,span节点可以用nextSibling获取
- parent.insertBefore(newElement,targetElement.nextSibling);
- }
- }
- //浏览器的兼容
- window.onload = function(){
- var body = document.getElementsByTagName('body')[0];
- if(BrowserDetect.browser == 'Internet Explorer' && BrowserDetect.version <= 7){
- var input = document.createElement("<input type=\"radio\" name=\"sex\">");
- }else{
- var input = document.createElement('input');
- input.setAttribute('type','radio');
- input.setAttribute('name','sex');
- }
- body.appendChild(input);
- };
- //replaceChild()方法用新节点替换某个子节点。
- window.onload = function(){
- var span = document.getElementsByTagName('span')[0];
- var em = document.createElement('em');
- span.parentNode.replaceChild(em,span);
- };
- //cloneNode()方法克隆所有属性以及它们的值。
- window.onload = function(){
- var box = document.getElementById('box');
- var clone = filterWhiteNode(box).firstChild.cloneNode(true);
- //克隆一个节点,如果是true就是把标签内的文本也克隆,如果是flase,只是克隆标签
- box.appendChild(clone);
- };
- //移除空白字符
- function filterWhiteNode(node){
- for(var i = 0; i<node.childNodes.length; i++){
- if(node.childNodes[i].nodeType === 3 && /^\s+$/.test(node.childNodes[i].nodeValue)){
- node.childNodes[i].parentNode.removeChild(node.childNodes[i]);
- }
- }
- return node;
- }
- //removeChild()方法删除某个子节点。
- window.onload = function(){
- var box = document.getElementById('box');
- //box.removeChild(filterWhiteNode(box).firstChild); //删除box第一个子节点
- box.parentNode.removeChild(box);//删除整个box
- };
- //移除空白字符
- function filterWhiteNode(node){
- for(var i = 0; i<node.childNodes.length; i++){
- if(node.childNodes[i].nodeType === 3 && /^\s+$/.test(node.childNodes[i].nodeValue)){
- node.childNodes[i].parentNode.removeChild(node.childNodes[i]);
- }
- }
- return node;
- }
- window.onload = function(){
- //alert(Node); //IE不支持
- //if(xxx.nodeType === 1); //表示判断元素节点
- //if(xxx.nodeType === 3); //表示判断文本节点
- //alert(Node.ELEMENT_NODE); //表示元素节点的类型值
- //alert(Node.TEXT_NODE); //表示文本节点的类型值
- //if(xxx.nodeType === Node.ELEMENT_NODE);
- //if(xxx.nodeType === Node.TEXT_NODE);
- //if(xxx.nodeType === 元素);
- //if(xxx.nodeType === 文本);
- };
- if(typeof Node == 'undefined'){
- //创建一个全局Node
- window.Node={
- ELEMENT_NODE:1,
- TEXT_NODE:2
- };
- }
- alert(Node.ELEMENT_NODE);
- alert(Node.TEXT_NODE);
- window.onload = function(){
- //alert(document); //HTMLDocument
- //alert(document.nodeType); //9
- //alert(document.nodeValue); //null
- //alert(document.nodeName); //#document
- //alert(document.childNodes[0]); //DocumentType.IE理解为注释
- //alert(document.childNodes[0].nodeType); //10,IE为8
- //alert(document.childNodes[0].nodeName);//火狐为HTML,谷歌为html,IE为#document
- //alert(document.childNodes[1]); //HTMLHtmlElement
- //alert(document.childNodes[1].nodeType); //1
- //alert(document.childNodes[1].nodeName);
- };
- window.onload = function(){
- alert(document.documentElement); //HTMLHtmlElement
- alert(document.body.nodeName); //获取body
- alert(document.doctype); //DocumentType,IE会显示null
- };
- window.onload = function(){
- //alert(document.title);
- //document.title = 'box';
- alert(document.URL);
- alert(document.domain);
- alert(document.referrer);
- alert(document.images.length);
- };
- window.onload = function(){
- var box = document.getElementById('box');
- var text1 = document.createTextNode('Mr.');
- var text2 = document.createTextNode('Lee');
- box.appendChild(text1);
- box.appendChild(text2);
- box.normalize(); //合并同一级别的文本节点
- //alert(box.childNodes.length);
- alert(box.childNodes[0].nodeValue);
- };
- window.onload = function(){
- var box = document.getElementById('box');
- box.childNodes[0].splitText(3); //把前三个字符分离成新节点
- alert(box.childNodes[0].nodeValue);
- };
- window.onload = function(){
- var box = document.getElementById('box');
- //box.childNodes[0].deleteData(0,3); //把前三个字符删除
- //box.childNodes[0].insertData(0,'Hello!'); //添加字符串
- //box.childNodes[0].replaceData(0,2,'Miss'); //替换
- //alert(box.childNodes[0].substringData(0,2));
- //alert(box.childNodes[0].nodeValue);
- };
- window.onload = function(){
- //var box = document.getElementById('box');
- //alert(box.firstChild); //Comment
- //alert(box.firstChild.nodeType); //8
- //alert(box.firstChild.nodeValue); //我是注释
- var c = document.getElementsByTagName('!'); //IE支持,其他不支持
- alert(c[1].nodeValue);
- };
- window.onload = function(){
- if(document.compatMode == 'CSS1Compat'){
- alert(document.documentElement.clientWidth);
- }else{
- alert(document.body.clientWidth);
- }
- };
- window.onload = function(){
- //document.getElementById('box').scrollIntoView(); //将指定的节点滚动到可见范围内
- var box = document.getElementById('box');
- //alert(box.childNodes.length);
- //alert(box.children.length); //忽略掉了空白节点
- alert(box.children[0].nodeName);
- };
- window.onload = function(){
- var box = document.getElementById('box');
- var p = box.firstChild;
- //alert(box.contains(p)); //判断box是不是p的父节点
- //火狐旧版本不支持。新版本支持
- //safari2.x不支持,3.0以上才支持
- //很多更低的版本,提供的两个方案均不支持
- //alert(box.compareDocumentPosition(p)>16); //包含关系
- alert(contains(box,p));
- };
- //浏览器兼容
- function contains(refNode,otherNode){
- if(typeof refNode.contains == 'undefined'){
- return refNode.contains(otherNode);
- }else if(typeof refNode.compareDocumentPosition == 'function'){
- return refNode.compareDocumentPosition(otherNode)>16;
- }
- }
- window.onload = function(){
- var box = document.getElementById('box');
- //alert(box.innerText); //获取文本并过滤HTML,直接删掉。
- //box.innerText = '<b>123</b>'; //赋值并转义HTML
- alert(box.textContent);
- };
- window.onload = function(){
- var box = document.getElementById('box');
- //alert(document.getElementById('box').innerHTML); //获取文本(不过滤HTML)
- //document.getElementById('box').innerHTML = '<b>123</b>'; //可解析HTML
- document.getElementById('box').innerHTML = "<script>alert('')</script>"; //不能执行
- };
- window.onload = function(){
- var box = document.getElementById('box');
- //alert(box.outerText);
- //box.outerHTML = '<b>123</b>';
- box.outerText = '<b>123</b>';
- alert(document.getElementById('box')); //null表明<div>这个标签被抹去
- };
JS—DOM操作的更多相关文章
- JS DOM操作(创建、遍历、获取、操作、删除节点)
创建节点 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="u ...
- js dom 操作
JS的DOM操作 1DOM是文档对象模型,这种模型为树模型:文档是指标签文档,对象是指文档中每个元素:模型是指抽象化的东西. 2间隔与延迟间隔执行一段代码(函数):window.setInterv ...
- js——DOM操作(一)
DOM:Document Object Model 文档对象模型 文档:html页面 文档对象:页面中元素 文档对象模型:定义 为了能够让程序(js)去操作页面中的元素 DOM会把文档看作是一棵树 ...
- JS DOM操作 函数 事件 阻止事件冒泡
一 函数 1.字符串函数 s.tolowerCase( ): -- 变小写 s.toupperCase( ): -- 变大写 s.substr( 2 , 8 ): -- 截取 ...
- JS DOM操作(五) Window.docunment对象——操作元素
定位: var a = document.getElementByIt( "id" ) 同辈元素 var b = a.nextSibling; -- 找 a ...
- JS DOM操作(二) Window.docunment对象——操作样式
一 对元素的定位 在 js 中可以利用 id.class.name.标签名进行元素的定位 id.class 用在客户端 name 用在服务端 用 id 定位 -- ...
- JS DOM操作(一) 对页面的操作
DOM ——文档对象模型(Document Object Model)是表示和处理一个HTML或XML文档的常用方法. 在网页上,组织页面(或文档)的对象被组织在一个树形结构中,用来表示文档中对象的标 ...
- js dom 操作技巧
1.创建元素 创建元素:document.createElement() 使用document.createElement()可以创建新元素.这个方法只接受一个参数,即要创建元素的标签名.这个标签名在 ...
- js DOM操作练习
1.有如下html,如果用js获得被选中的option的text描述(非value)<select id="select_id"> <option vlue ...
随机推荐
- PHP开发-模板的使用
通过今天晚上半个多小时的赵老师对模板的介绍,大致对模板的使用.结构以及开发模板的思想有了个大致的了解. 为什么要开发模板:模板就是将PHP发过过程中使用到.需要操作的事物封装成对象.以便在使用到的时候 ...
- Power BI Premium
Power BI Premium的设计是为了满足拥有大量数据的大公司的需求.微软已经重新构建了Power BI的架构,以允许大量的“只读”用户.Premium用户还可以利用很多新功能. Power B ...
- Java中的package和Import关键字的作用
一.package关键的作用 1.包的概念 package翻译为包,本质上就是一个文件夹,通过包组织项目结构的过程,就是通过包对应的文件夹管理.java和.class文件的过程. 2.包的作用 通过包 ...
- (六--二)scrapy框架之持久化操作
scrapy框架之持久化操作 基于终端指令的持久化存储 基于管道的持久化存储 1 基于终端指令的持久化存储 保证爬虫文件的parse方法中有可迭代类型对象(通常为列表or字典)的返回,该返回值可以通过 ...
- mybatis基础CURD的学习
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "- ...
- s5pc100开发板linux内核移植
相关软件下载地址:http://pan.baidu.com/s/16yo8Y 应用于FSC100开发板 交叉编译工具:arm-cortex_a8-linux-gnueabi-gcc linux-2.6 ...
- 集合框架的详解,List(ArrayList,LinkedList,Vector),Set(HashSet,TreeSet)-(14)
集合详解: /* Collection |--List:元素是有序的,元素可以重复.因为该集合体系有索引. |--ArrayList:底层的数据结构使用的是数组结构.特点:查询速度很快.但是增删稍慢. ...
- 三十、CI框架之使用cookies实现用户登录和退出。
一.在控制器中,写入3个函数.一个是login用于登录,一个是logout用于退出,一个show用来展示界面. login代码如下: logout和showuser函数如下: 二.我们的登录界面 三. ...
- Day 5 :ArrayList原理、LinkedList原理和方法和迭代器注意事项
迭代器在变量元素的时候要注意事项: 在迭代器迭代元素 的过程中,不允许使用集合对象改变集合中的元素个数,如果需要添加或者删除只能使用迭代器的方法进行操作. 如果使用过了集合对象改变集合中元素个数那 ...
- Ubuntu下运行python文件
方法一: 直接> python2.7/python3.6 test.py 方法二: 在文件首行写上 #!/usr/bin/python3 这个是配置的路径 也可以通过 > which p ...