第五模块:WEB开发基础-第5,6章 BOM,DOM

系列文章目录

BOM,DOM



前言

BOM和DOM


一、BOM

1.BOM对象

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>BOM对象</title>
</head>
<body>
	<!-- 浏览器对象模型 BOM-->
	<!-- 
		1.window
		  alert()
		  confirm()
		  prompt()

		  setInterval()
		  setTimeout()
		2.location
		  href
		  hash
		  url
		  ...
		  reload()
		3.screen
		4.history
		  go()
	 -->
</body>
</html>

2.window对象的alert,confirm,prompt

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>alert|confirm|prompt</title>
</head>
<body>
	<script type="text/javascript">
		// window.alert("mjj");

		// var a = window.confirm("你确定要离开网站吗");
		// console.log(a);

		// var name = window.prompt("请输入你吃了什么","mjj");
		// console.log(name);
	</script>
</body>
</html>

3.定时器方法

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>setTimeout()|setInterval()</title>
</head>
<body>
	<script type="text/javascript">
		//延迟性操作
		// window.setTimeout(function(){
		// 	console.log("mjj")
		// },50);
		// console.log("xiongda")
		var num = 0;
		var timer = null;
		timer = setInterval(function(){
			num++;
			if (num>5){
				clearInterval(timer);
				return;
			}
			console.log("num:" + num);
		},1000);
	</script>
</body>
</html>

4.location对象的常用属性介绍

location.hostname
""
location.href
"file:///D:/h5file/BOM/4location%E5%AF%B9%E8%B1%A1.html"
location.pathname
"/D:/h5file/BOM/4location%E5%AF%B9%E8%B1%A1.html"
location.port
""
location.protocol
"file:"
location.search
""

5.如何访问每个查询字符串参数

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>如何访问每个查询字符串参数</title>
</head>
<body>
	<script type="text/javascript">
		/*
		var args = {
			user:"mjj",
			pwd:"123"
		}
		*/
		function getQueryString(){
			//1.取得去掉?的查询的字符串
			var qs = location.search.length > 0?location.search.substring(1):"";
			//2.取得每一项,存放到数组中
			var items = qs.length ? qs.split("&"):[];

			var item = null,name=null,value=null,args={};
			for (var i = 0;i < items.length;i++){
				item = items[i].split("=");//['name':'mjj']
				name = decodeURIComponent(item[0]);
				value = decodeURIComponent(item[1]);
				if (name.length){
					args[name] = value;
				}
			}
			return args;
		}
		var args = getQueryString();
		console.log(args.user);
		console.log(args.pwd);

	</script>
</body>
</html>

6.当前浏览器的位置操作

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>位置操作</title>
</head>
<body>
	<script type="text/javascript">
		console.log("mjj");
		//2秒之后跳转小猿圈web页面
		setTimeout(function(){
			// location.href = "https://www.baidu.com/";
			// location.replace("https://www.baidu.com/");
			// location.reload();
		},2000);
	</script>
</body>
</html>

7.如何检测当前浏览器上的插件

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>navigator对象</title>
</head>
<body>
	<script type="text/javascript">
		console.log(navigator.plugins);
		function hasPlugin(name){
			//如果有插件,返回true.反之亦然
			name = name.toLowerCase();
			for (var i=0;i<navigator.plugins.length;i++){
				if (navigator.plugins[i].name.toLowerCase().indexOf(name)>-1){
					//有此插件
					return true;
				}else{
					return false
				}
			}
		}
		alert(hasPlugin("Flash"));
		alert(hasPlugin("Chrome PDF Plugin"));
	</script>
</body>
</html>

8.history对象的介绍

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>history对象</title>
</head>
<body>
	<script type="text/javascript">
		// console.log(history);
		var count = 0
		setTimeout(function(){
			count ++;
			console.log(count);
			history.go(-1)//刷新

		},2000)
	</script>
</body>
</html>

二、DOM

1.快速认识

DOM
document object module文档对象模型

js对象分类3钟:
1.用户定义对象
2.内建对象 array date math
3.宿主对象

model map(地图)

dom看作一棵树
dom把文档看作一棵家谱树
parent child sibling

2.DOM中节点的分类

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>2节点</title>
</head>
<body>
	<!-- 节点 node -->
	<!-- 1.元素节点(element node) -->
	<!-- 2.文本节点(text node) -->
	<!-- 3.属性节点(attribute node) -->

	<!-- 没有内容的文档是没有任何价值的,大多数内容都是文本来提供的 -->
</body>
</html>

3.获取元素节点对象的方式

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title class="item">class list</title>
</head>
<body>
	<h2>你要买什么课程</h2>
	<ul id="classList">
		<li>js</li>
		<li>css</li>
	</ul>
	<!-- 节点类型:
		1.元素节点
	    2.文本节点
	    3.属性节点
	 -->
	 <script type="text/javascript">
	 	//1.document.getElementById() 单个对象
	 	var eleNode = document.getElementById("classList");
	 	console.log(eleNode);
	 	console.log(typeof eleNode);
	 	//2.document.getElementsByTagName() 获取出来的是一个节点对象集合,有点像数组
	 	var oList = document.getElementsByTagName("li");
	 	console.log(oList.length);
	 	for (var i = 0;i < oList.length;i++){
	 		console.log(oList[i]);
	 	}
	 	console.log(typeof oList);
	 	//3.document.getElementsByClassName("item") 获取出来的是一个节点对象集合
	 	var oItems = document.getElementsByClassName("item");
	 	console.log(oItems);
	 </script>
</body>
</html>

4.setAttribute和getAttribute

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>class list</title>
</head>
<body>
	<h2>你要买什么课程</h2>
	<p title="选择课程" id="box">值得购买</p>
	<ul id="classList">
		<li>js</li>
		<li>css</li>
	</ul>

	<script type="text/javascript">
		var op = document.getElementsByTagName("p")[0];
		//获取属性值,有一个必须的参数,这个节点对象的名字
		var title = op.getAttribute("title")
		console.log(title);
		//获取属性值 setAttribute(name,value)
		op.setAttribute("id","box");
	</script>
</body>
</html>

5.节点属性

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>节点属性</title>
</head>
<body>
	<!-- nodeName nodeValue nodeType -->
	<div id="box" title="我是title">我是一个文本节点 <!-- 我是注释 --></div>
	<script type="text/javascript">
		//1.元素节点
		var oDiv = document.getElementById("box");
		console.log(oDiv.nodeName + "|" + oDiv.nodeValue+ "|" + oDiv.nodeType);
		//2.获取属性节点
		var attrNode = oDiv.attributes[0];
		console.log(attrNode.nodeName + "|" + attrNode.nodeValue+ "|" + attrNode.nodeType);
		//3.获取文本节点
		var textNode = oDiv.childNodes[0];
		console.log(textNode.nodeName + "|" + textNode.nodeValue+ "|" + textNode.nodeType);
		//4.获取注释节点
		var commentNode = oDiv.childNodes[1];
		console.log(commentNode.nodeName + "|" + commentNode.nodeValue+ "|" + commentNode.nodeType);

		console.log(document.nodeType);

	</script>
</body>
</html>

6.节点对象的常用属性

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>节点对象的其他常用属性</title>
</head>
<body>
	<div class="previous">我是上个兄弟</div>
	<div id="father">
		<p>mjj1</p>
		<p>mjj2</p>
	</div>
	<div class="sibling">我是兄弟</div>
	<script type="text/javascript">
		var ofather = document.getElementById('father');
		console.log(ofather.childNodes[0].nodeType);

		console.log(ofather.childNodes[0])
		console.log(ofather.firstChild);

		console.log(ofather.childNodes[ofather.childNodes.length - 1])
		console.log(ofather.lastChild);
		
		console.log(ofather.parentNode);
		console.log(ofather.nextSibling);
		console.log(ofather.previousSibling);
	</script>
</body>
</html>	

7.节点对象属性在各浏览器的兼容处理

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>节点对象的其他常用属性</title>
</head>
<body>
	<div class="previous">我是上个兄弟</div>
	<div id="father">
		<p>mjj1</p>
		<p>mjj2</p>
	</div>
	<div class="sibling">我是兄弟</div>
	<script type="text/javascript">
		var ofather = document.getElementById('father');
		/*console.log(ofather.childNodes[0].nodeType);

		console.log(ofather.childNodes[0])
		console.log(ofather.firstChild);

		console.log(ofather.childNodes[ofather.childNodes.length - 1])
		console.log(ofather.lastChild);

		console.log(ofather.parentNode);
		console.log(ofather.nextSibling);
		console.log(ofather.previousSibling);*/

		function get_childNodes(fatherNode){
			var nodes = fatherNode.childNodes;
			var arr = [];//保存已经获取的元素节点对象
			for(var i = 0;i < nodes.length;i++){
				if (nodes[i].nodeType == 1){
					arr.push(nodes[i]);
				}
			}
			return arr
		}

		var childnodes = get_childNodes(ofather);
		console.log(childnodes);

		function get_nextSibling(n){
			var x = n.nextSibling;
			while(x && x.nodeType != 1){
				x = x.nextSibling;
			}
			return x;
		}
		
		console.log(get_nextSibling(ofather));
	</script>
</body>
</html>

8.节点对象的增删改查方法

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>7节点方法</title>
	<style type="text/css">
		.active{
			color: red;
			font-size: 30px;
		}
	</style>
</head>
<body>
	<div id="box">
		<p id="active">mjj</p>
		<p>alex</p>
	</div>
	<script type="text/javascript">
		var oDiv = document.getElementById('box');
		var oActive = document.getElementById('active');
		var newNode = document.createElement("p");
		var newNode2 = document.createElement("p");
		var newNode3 = document.createElement("a");

		newNode.innerHTML = "alex@qq.com";
		newNode.innerHTML = '<a href="#">mjj@qq.com</a>';


		newNode.setAttribute("class","active");
		oDiv.appendChild(newNode);
		//第一个参数是新插入节点,第二个参数是参考节点
		oDiv.insertBefore(newNode2,oActive);
		newNode3.setAttribute("href","http://www.baidu.com");
		newNode3.innerHTML = "百度一下";

		/*
		var textNode = document.createTextNode("alex");
		newNode.appendChild(textNode);
		*/
		// newNode.innerText = "alex@qq.com";
		// newNode = null;//释放对象

		//oDiv.removeChild(oActive); 删除操作

		oDiv.replaceChild(newNode3,oActive);
	</script>
</body>
</html>

9.样式设置

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>动态操作样式</title>
	<style type="text/css">
        .high{
            font-size: 30px;
            width: 250px;
            height: 250px;
            background-color: black;
            color: white;
            text-align: center;
            line-height: 250px;
        }
	</style>
</head>
<body>
	<p id="box" class="high">mjj</p>

	<script type="text/javascript">
		//nodeObj.style
		var op = document.getElementById('box');
		console.log(op.style);
		//1.直接操作样式属性
		/*
		op.style.color = "white";
		op.style.backgroundColor = "black";
		op.style.width = "250px";
		op.style.height = "250px";
		op.style.textAlign = "center";
		op.style.lineHeight = "250px";
		op.style.fontSize = "30px";
		*/
		//2.通过控制属性的类名来控制样式
		op.setAttribute("class","high");

	</script>
</body>
</html>

10.事件介绍和onclick事件

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>事件</title>
	<style type="text/css">
		#box{
			width: 100px;
			height: 100px;
			background-color: blue;
		}
	</style>
</head>
<body>
	<!-- <div id="box" οnclick="add()"></div> -->
	<div id="box""></div>
	<script type="text/javascript">
		var oDiv = document.getElementById('box');

		var isBlue = true;
		oDiv.onclick = function(){
			// alert("事件被触发了");
			// console.log(this);
			// this 指向了当前的元素节点对象
			if(isBlue){
				this.style.backgroundColor = "red";
				isBlue = false;
			}else{
				this.style.backgroundColor = "blue";
				isBlue = true;
			}
		};

		// oDiv.onclick = function(){
		// 	alert("事件被触发了");
		// }
	</script>
</body>
</html>

11.鼠标悬浮事件

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>onmouseover和onmouseout事件</title>
	<style type="text/css">
		#box{
			width: 200px;
			height: 200px;
			background-color: red;

		}
	</style>
</head>
<body>
	<div id="box"></div>
	<script type="text/javascript">
		//1.找开关 2.按一下 3.灯亮了

		//1.找到触发的事件对象 2.事件 3.事件处理程序
		var oDiv = document.getElementById('box');
		//2.鼠标滑过事件
		oDiv.onmouseover = function(){
			//3.事件处理程序
			this.style.backgroundColor = "green";
		}

		//2.鼠标移开事件
		oDiv.onmouseout = function(){
			//3.事件处理程序
			this.style.backgroundColor = "red";
		}
	</script>
</body>
</html>

12.光标聚焦和失焦事件

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>表单控件上的事件</title>
	<style type="text/css">
		.text{
			color: red;
			font-size: 12px;
		}
	</style>
</head>
<body>
	<form action="">
		<p class="name">
			<label for="username">用户名:</label>
			<input type="text" name="user" id="username">
		</p>
		<p class="pwd">
			<label for="pwd">密码:</label>
			<input type="password" name="pwd" id="pwd">	
		</p>
		<input type="submit" name="">
	</form>
	<script type="text/javascript">
		var userName = document.getElementById('username');
		var newNode = document.createElement("span");
		userName.onfocus = function(){
			// console.log("请输入用户名");
			newNode.innerHTML = "请输入用户名";
			newNode.setAttribute("class","text")
			userName.parentNode.appendChild(newNode);
		}
		userName.onblur = function(){
			newNode.innerHTML = "请输入正确的用户名";
			newNode.setAttribute("class","text")
			userName.parentNode.appendChild(newNode);
		}
	</script>
</body>
</html>

13.表单控件上内容选中和改变事件

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>内容选中和改变事件</title>
</head>
<body>
	<!-- onselect onchange -->
	<textarea cols="30" rows="10">请写入个人简介,字数不少于200字</textarea>
	<input type="text" name="" value="mjj">
	<script type="text/javascript">
		var textarea = document.getElementsByTagName('textarea')[0];
		var inputObj = document.getElementsByTagName('input')[0];
		textarea.onselect = function (){
			console.log("内容被选中了");
		}
		inputObj.onchange = function(){
			console.log("内容被改变了");
		}
		inputObj.oninput = function(){
			console.log("内容被实时的改变了");
			console.log(this.value);
		}
	</script>
</body>
</html>

14.窗口加载事件

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>窗口加载事件</title>
	<script type="text/javascript">
		/*
		setTimeout(function(){
			var oDiv = document.getElementById('box');
			oDiv.onclick = function(){
				this.innerHTML = "alex";
			}
		},2000);
		*/
		//等待文档元素加载完成后才会调用onload
		window.onload = function(){
			var oDiv = document.getElementById('box');
			oDiv.onclick = function(){
				this.innerHTML = "alex";
			}
		}
	</script>
</head>
<body>
	<div id="box">mjj</div>
</body>
</html>

总结

bom和dom操作

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值