JS基础9(DOM2)

Attribute系列方法

  1. 三种方法
    set/getAttribute()
    removeAttribute()
    【注】都是操作当前元素节点中某个属性的
window.onload = function(){
	var oDiv = document.getElementById("div1");
	//alert(oDiv.title);//hello
	//alert(oDiv.className);//box
	alert(oDiv.getAttribute("title"));//hello

	//可以进行修改
	//oDiv.title = "xxx";
	//oDiv.className = "box3";
	oDiv.setAttribute("title","xxx");

/*
	上述两种方式的区别
	<1>class属性范围区别,点操作是通过className,而set/getAttribute是通过class
	<2>set/getAttribute是用户自定义属性支持。
	<3>removeAttribute
*/	alert(oDiv.getAttribute("class"));//box
oDiv.setAttribute("class","yyy");
	
	//设置用户自定义属性
	oDiv.xxx = "yyy";//这种情况 并不会添加属性
	oDiv.setAttribute("xxx","yyy");

	oDiv.removeAttribute("title");//此时删除了属性title
	oDiv.title = "";//不能将属性删除,只能将属性置空

}
<div id = "div1" title="hello" name = "world" class = "box"></div>
  1. 元素节点属性
    (1)childNodes 获取当前元素节点的所有的子节点,包括两种节点类型
    1、元素节点
    2、文本节点
window.onload = function(){
	var oDiv = document.getElementById("div1");
	alert(oDiv.childNodes);//[object NodeList]  装有当前元素节点所有的子节点
	alert(oDiv.childNodes.length);//3
}
<div id = "div1"><em>斜体</em>文本内容<strong>粗体</strong></div>

(2)DOM节点类型
元素节点 文本节点 属性节点
【注】节点可以分为元素节点、属性节点和文本节点,而这些节点又有三种非常有用的属性,分别为 nodeName、nodeType 和 nodeValue
在这里插入图片描述

window.onload = function(){
//元素节点				    alert(oDiv.childNodes[0].nodeName);//EM
alert(oDiv.childNodes[0].nodeType);//1
alert(oDiv.childNodes[0].nodeValue);//null

//文本节点
alert(oDiv.childNodes[1].nodeName);//#text
alert(oDiv.childNodes[1].nodeType);//3
alert(oDiv.childNodes[1].nodeValue);//文本内容

}
<div id = "div1"><em>斜体</em>文本内容<strong>粗体</strong></div>

(3)firstChild 快速找到当前元素节点子节点的第一个
lastChild 快速找到元素节点子节点的最后一个

window.onload = function(){
alert(oDiv.firstChild.nodeName);//EM
alert(oDiv.lastChild.nodeName);//STRONG
}
<div id = "div1"><em>斜体</em>文本内容<strong>粗体</strong></div>
【注】包括回车  换行 tab键  空格  字符
window.onload = function(){
	var oDiv = document.getElementById("div1");
	alert(oDiv.childNodes.length)//5
}

<div id = "div1">
	<em>斜体</em>
	文本内容
	<strong>粗体</strong>
</div>
【注】包括回车  换行 tab键  空格  字符
window.onload = function(){
	var oDiv = document.getElementById("div1");
	/*【问题】如何将空白节点取出
	【注】识别出空白节点
	/^\s+$/.test()   使用左侧的正则去进行验证,如果是空白节点,返回true,否则返回false
	*/
	alert(/^\s+$/.test("   "));//true
	alert(/^\s+$/.test("1   "));//true
	alert(oDiv.childNodes.length);//5
	var nodes = removeSpceNode(oDiv.childNodes);
	alert(nodes.length);//3
}
//删除空白节点
function removeSpaceNode(nodes){
	var result = [];//用来存放不是空白节点的节点
	for(var i = 0;i<nodes.length;i++){
		//判断是否是空白节点
		if(nodes[i].nodeType == 3 &&  /^\s+$/.test(nodes[i].nodeValue)){
			continue;
		}
		result.push(nodes[i]);
	}
	return result;
}
<div id = "div1">
	<em>斜体</em>
	文本内容
	<strong>粗体</strong>
</div>

使用firstChild lastChild
必须从父节点上删除空白节点

window.onload = function(){
	var oDiv = document.getElementById("div1");
	removeSpaceNode2(oDiv);
	//alert(oDiv.childNodes.length);//3
	//alert(oDiv.firstChild.nodeName);//EM
//【注】删除数组元素的时候,必须倒序删除
	}
	function removeSpaceNode2(parent){
		var nodes = parent.childNodes;
		for (var i = nodes.length -1 ;i>= 0;i--){
		if(node[i].nodeType == 3 && /^\s+$/.test(nodes[i].nodeValue)){
		//删除该空白节点
		parent.removeChild(nodes[i]);
		}
		}
	}

<div id = "div1">
	<em>斜体</em>
	文本内容
	<strong>粗体</strong>
</div>

(4)ownerDocument属性
【注】返回该节点的文档对象根节点,返回的对象相当于document
alert(box.ownerDocument === document);//true 根节点

window.onload = function(){
	var oDiv= document.getElementById("div1");
	alert(oDiv.ownerDocument == document);//true
}

<div id = "div1"></div>

(5)parentNode 属性返回该节点的父节点
previousSibling 属性返回该节点的前一个同级节点
nextSibling 属性返回该节点的后一个同级节点

window.onload = function(){
	var oDiv= document.getElementById("div1");
	//alert(oDiv.parentNode.nodeName);//BODY
removeSpaceNode2(oDiv.parentNode)	//alert(oDiv.previousSibling.nodeName);//SPAN
alert(oDiv.nextSibling.nodeName);//P
}
function removeSpaceNode2(parent){
		var nodes = parent.childNodes;
		for (var i = nodes.length -1 ;i>= 0;i--){
		if(node[i].nodeType == 3 && /^\s+$/.test(nodes[i].nodeValue)){
		//删除该空白节点
		parent.removeChild(nodes[i]);
		}
		}
	}

<span></span>
<div id = "div1"></div>
<p></p>

(5)属性节点 attribute
返回该节点的属性节点【集合】
集合特点:1、不重复 2、无序

属性
attributes.length 返回属性节点个数

window.onload = function(){
	var oDiv = document.getElementById("div1");
	alert(oDiv.attributes);//[object NameNodeMap]
	alert(oDiv.attributes.length);//4

	//访问其中一个属性节点
alert(oDiv.attributes.getNamedItem("id"));//object Attr
alert(oDiv.attributes["id"]);//object Attr

	//属性节点
	nodeName   nodeType  nodeValue
	  属性名		 类型2alert(oDiv.attributes["id"].nodeName);//id
	alert(oDiv.attributes["id"].nodeType);//2
	alert(oDiv.attributes["id"].nodeValue);//div1
}

<div id = "div1" title = "hello" name = "world" class = "box"></div>

(6)节点操作
①document.write()
【注】document.write()添加内容的时候, 会将原有的页面上的内容覆盖掉

#div1{
	width:300px;
	height:300px;
	background:red
}

window.onload = function(){
	var oBtn = document.getElementById("btn");
	oBtn.onclick = function(){
		document.write("<h1>我是标题内容</h1>");
		//【注】document.write()添加内容的时候, 会将原有的页面上的内容覆盖掉
	}
}

<div id = "div1"></div>
<button id = "btn"></button>

createElement()
【格式】document.createElement(标签名);
appendChild()
【格式】parent.appendChild(newNode)
功能:将newNode插入到parenet子节点的末尾
createTextNode()
【格式】document.createTextNode(文本)
功能:创建文本节点
【注】创建一个带文本的元素节点(封装)

window.onload = function(){
	var oBtn = document.getElementById("btn");
	var oDiv = document.getElementById("div1");
	oBtn.onclick = function(){
	//1、创建span节点
	var node = document.createElement("span");
	alert(node);//只是进行了创建该节点,并没有插入该节点
	oDiv.appendChild(node);//创建了该节点,并把它插入到了div1中,但是并没有文本信息
	//2、给span节点中添加文本
	var oText = document.createTextNode("文本内容");
	//3、将文本插入到节点中
	node.appendChild(oText);
	//4、将节点插入到div中
	oDiv.appendChild(node);
	}
}

<div id = "div1">
	<p>p</p>
	<em>斜体</em>
</div>
<button id = "btn"></button>
创建一个带文本的元素节点(封装)
window.onload = function(){
	var oDiv = document.getElementById("div1");
	oBtn.onclick = function(){
		var node = createElementWithTxt("span","文本内容");
		oDiv.appendChild(node);
	}
}

function createElementWidthTxt(tagName,txt){
	var node = document.createElement(tagName);
	var oTxt = document.createTextNode(txt);
	node.appendChild(oTxt);
	return node;
}

<div id = "div1">
	<p>p</p>
	<em>斜体</em>
</div>
<button id = "btn"></button>

③insertBefore()
【格式】父节点.insertBefore(插入的节点,旧节点);
功能:将插入的节点插入到旧节点之前

window.onload = function(){
	//创建一个<strong>  将这个节点插入到span节点之前
//<1>创建strong
var node = createElementWithTxt("strong","strong文本");

var oSpan = document.getElementByTagName("span")[0];
//<2>进行插入
oSpan.parentNode.insertBefore(node,oSpan);//strong节点插入到span节点之前

insertAfter(node,oSpan);
【注】DOM里面insetAfter()这个方法是没有的,可以自己去封装
}

function createElementWidthTxt(tagName,txt){
	var node = document.createElement(tagName);
	var oTxt = document.createTextNode(txt);
	node.appendChild(oTxt);
	return node;
}

function insertAfter(newNode,oldNode){
	//判断oldNode是否是最后一个节点
	var parent = oldNode.parentNode;
	if(oldNode == parent.lastChild){
		//最后一个节点,直接插入到子节点的末尾
		parent.appendChild(newNode);
	}else{
		//插入到oldNode的下一个节点之前
		parent.insertBefore(newNode,oldNode.nextSibling);
	}
}

<div id = "div1">
	<p>p</p>
	<span>span</span>
	<em>em</em>
</div>

④replaceChild()
【格式】parent.replaceChild(newNode,oldNode);
功能:用newNode将oldNode给替换掉

cloneNode()
【格式】node.cloneNode()
返回值:新克隆出来的节点
参数:true 默认是false 如果传true,就会复制元素节点中的innerHTML

removeChild()
【格式】node.parentNode.removeChild(node);
功能:删除节点

window.onload = function(){
 var oDiv = document.getElementById("div1");
 var node = document.createElement("strong");

 //oDiv.parentNode.replaceChild(node,oDiv);//原来的div直接替换成strong标签
  var oDiv = document.getElementById("div1");
  var oSpan = document.getElementByTagName("span")[0];
  var node = oDiv.cloneNode(true);
  //将div添加到span节点的前面
  oSpan.parentNode.insertBefore(oDiv,oSpan);

  oDiv.parentNode.removeChild(oDiv);
}

<span>span</span>
<div id= "div1">div</div>
<p>p</p>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值