html bom与dom解析

1 安排

html/cssJavaScript基础

JavaScript bom & dom

JavaScript 

javaweb

JavaScript 高级:ajax

ssh

JavaScript 高级:面对对象

 

回顾

l html 使用 JavaScript 结合

方式1<script type="text/javascript">js 代码</script>

方式2:引入外部js类库,<script type="text/javascript" src="....js"></script>

l 变量声明

格式:var 变量;

var 可以省略的,但不能写错。例如:错误 String username = "";

变量名称:字母、_$ 开头,不能使数字开发

l JavaScript 原始数据类型:5

string   必须使用引号(双单)

number 整形、浮点

boolean true|false

null 占位符,用于对象返回的  typeof null --> Object

undefined 变量已经声明,但没有初始化,例如:var demo;

undefined null衍生 出来的。 null==undefined  --> true

l 运算符

一元:i++、++i、--i、--i、 +i 、 -i

位运算:一般不用,优化,java一样的。

boolean 运算(逻辑)

String --> boolean 非空字符串都是true例如:"false"  --> true

number --> boolean +0  -0  NaN false ,其他都是true

null --> boolean  : false

undefined --> boolean : false

Object --> 非空都是true

使用等值

"" == true -->  false

"true" == true  -->  false

1 == false --> false

0 == false  --> true

2 == true  --> true 表示1false 表示0 2==1

NaN == NaN  --> false 任何数据与NaN操作都是 false

NaN != NaN  --> true

"NaN" == NaN

//等值运算,尝试将内容转换 数字,再进行运算

//alert("" == true); //

//alert("true" == true);  //

//alert("1" == true );  //

alert("0" == true );  //

parseInt("abc")  --> NaN

alert(NaN == true)  --> false

 

l 引用类型

Object , toString() 程序字符串答应

Function 函数

定义方式:

方式1function xxx(参数列表){标签体} ;  xxx();

方式2var fn = new Function(参数列表,,,,"标签体");

方式3var fn = function(){};   fn()

注意:

只要使用函数名就可以进行操作。不需要明确实际参数

fn()  fn("abc")  fn(132)

如果将函数赋值给其他变量,其他变量仍然可以执行函数,相当于将函数引用进行传递

fn ;  var myfn = fn;    myfn()

函数体中获得实际参数,arguments 获得所有的实际参数,是一个数组

return 可以进行返回,如果没有返回,默认值undefined

void 运算符  void(函数| 函数体)  ,忽略函数返回值

String/Number/Boolean  原始类型进行操作

new String(...)  返回对象

String(...) 返回字符串--原始类型

Array 数组

方式1:使用中括号,var x = [] ;

方式2指定大小值, var y = new Array(5);

方式3:初始化,var z = new Array("","","",134,funciton(){})

注意:

数组的长度可以变

数组成员可以任意

Date 时间

new Date()

getFullYear()

getYear()

getMonth()

getDay()

....

toLocaleString()

Math

PI

Global 全局变量

eval  将字符串进行解析,并执行。

parseInt parseFloat  处理整形和浮点

parseInt("123abc")  --->  123

encodeURI /  decodeURI()  不处理特殊字符

encodeURIComponent()  / decodeURI... 处理特殊字符

RegExp 正则

对象:

方式1var xx = new RegExp("表达式","gi")

g 全局 global i 忽略大小写  ignore Case

方式2var yy = /xxx/gi

正则表达式语法:^...$

方式:exec()  获得匹配位置、test() 是否匹配

字符串:

match() 匹配

search() 查询

replace 替换

split 分隔

 

BOM

3.1 介绍

l browser object model :浏览器对象模型,ECMAScript 规范规定,用于描述浏览器行为。提供对象进行相应操作。

 

l 学习:windowhistorylocation

window:表示窗体

history:浏览记录

location:当前访问路径

screen:浏览器屏(了解)

navigator 浏览器特性(了解)

3.2 window 窗体

l 每一个html对应一个window对象。

frameset 框架

3.2.1 window集合

l 提供frames[] 数组获得所有的框架

3.2.2 window属性

l 导航定位

self :当前操作窗口

parent:当前的父窗口

top:所有的顶级窗口

l bom其他对象引用

例如:history引用,window.history

l opener 确定有谁打开当前页面,在子窗口获得父窗口

A.html 打开 B.html ,此时在B.opener 获得就是Awindow对象。

3.2.3 window 方法

3.2.3.1 打开窗口

l open方式

格式:window.open(URL,name,features,replace)

URL:确定打开路径

name:一般没有,设置以什么方式打开。如果不写,默认:_blank

features : 用于确定新窗口特性,例如:大小,是否有地址栏等

大部分值不同的浏览器实现不同,常用:heightwidth

注意:多个值之间使用逗号分隔,每一个值格式:k=v

replace : 打开新窗口是否替换历史记录,一般没用。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
	<script type="text/javascript">
		
	
		//事件--指定操作进行时,js函数将触发
		// * onclick 目标点击时将触发事件
		
		var newWindowObject;
		function openNewWindow(){
			//打开新窗口
			newWindowObject = window.open("03.window target.html","","height=100px,width=100px");
		}
		
		function closeNewWindow(){
			newWindowObject.close();
		}
		
	</script>
</head>
<body>
	<input type="button" value="打开新窗口" οnclick="openNewWindow()" /> <br/>
	<input type="button" value="关闭新窗口" οnclick="closeNewWindow()" /> <br/>
	
	<div id="divId">
			xxxx
	</div>
	
	<script type="text/javascript">
		//程序加载时,修改div内容
		window.document.getElementById("divId").innerHTML = "abc";
	</script>
</body>
</html>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
	<script type="text/javascript">
		function closeSelf(){
			//当前窗口关闭
			window.close();
		}
		
		function sendParent(){
			//window.opener 父窗口的window
			window.opener.document.getElementById("divId").innerHTML = "算你屌";
		}
	</script>
</head>
<body>
	我被打开了 <br/>
	<input type="button" value="自己将关闭" οnclick="closeSelf()" /> <br/>
	<input type="button" value="给父窗口写内容" οnclick="sendParent()" /> <br/>
</body>
</html>


3.2.3.2 消息框

警告框:window.alert("内容"); 提供确定按钮

提示框:window.confirm("内容") 提供 确定 和 取消按钮

点击确定,返回值:true

点击却小,返回值:false

确定框:window.prompt("内容","默认") 提供 确定 、 取消 和 文本框进行输入

确认 返回值:输入值

取消,返回值:null

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
	<script type="text/javascript">
		//警告
		//alert("game over!!!");
		//提示
		//var returnVal = confirm("确定要退出游戏吗?");
		//alert(returnVal);
		//确定框
		//var returnVal2 = prompt("请输入您的性别:\n1.男 \n2.女","1");
		//alert(returnVal2);
		
		//以上所有消息框,都可以使用\n 进行回车换行
	</script>
</head>
<body>

</body>
</html>


3.2.3.3 指定时间执行 Timing

setInterval 指定js函数,按照指定时间,循环执行。

格式:setInterval(函数,时间)

函数:两种写法,函数名,"函数名()"

时间:单位毫秒

clearInterval setInterval开启定时器关闭。

格式:clearInterval(对象)

 

setTimeout  在执行时间,延迟执行一次js函数。

clearTimeout 停止setTimeout生成定时器。

 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>

</head>
<body>
	<div id="divId">...</div><br/>
	
	<input type="button" value="停止时间" οnclick="stopDate()"/>
	<input type="button" value="开始时间" οnclick="startDate()"/>
	
	<script type="text/javascript">
		//setInterval();
		
		//显示
		function showDate(){
			//1 创建时间
			var date = new Date();
			//2 指定位置显示
			document.getElementById("divId").innerHTML = date.toLocaleString();
		}
		//执行一次
		showDate();
		//每秒执行一次
		//setInterval(showDate,1000);
		var interId;
		
		//开始
		function startDate(){
			if(! interId){  //如果没有赋值 undefined --> false ; 有值Object --> true  (null -- false)
				showDate();
				interId = setInterval("showDate()",1000);
			}
		}
		
		//停止
		function stopDate(){
			if(interId) {
				clearInterval(interId);
				interId = undefined;
			}
		}
		
		
		
		
		
		
	</script>
</body>
</html>

3.3 history历史

l History 对象包含用户(在浏览器窗口中)访问过的 URL

l length 属性:表示历史记录个数。

back() 回退 左边按钮

forward() 前进 右边按钮

go(number | url)  跳到指定的位置

-1 等效于back()

1 等效于forward()

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
	<script type="text/javascript">
	</script>
</head>
<body>
	<a href="06.第2个页面.html">打开2页面</a> <br/>
	
	<input type="button" value="前进" οnclick="history.forward()"> <br/>
	<input type="button" value="前进" οnclick="history.go(1)"> <br/>
</body>
</html>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
	<script type="text/javascript">
	</script>
</head>
<body>
	<input type="button" value="返回" οnclick="history.back()"> <br/>
	<input type="button" value="返回" οnclick="history.go(-1)"> <br/>
	<a href="javascript:void(0)" οnclick="history.go(-1);">返回</a> <br/>
</body>
</html>


3.4 Location

l Location 对象包含有关当前 URL 的信息。

3.4.1 属性

l URL路径:  http://www.itheima.com:80/a/b/c/demo.html?username=jack&password=1234

hash 设置或返回从井号 (#) 开始的 URL(锚)。(了解)

   index.html#xxx  获得xxx

host 设置或返回主机名和当前 URL 的端口号。

   www.itheima.com:80

hostname 设置或返回当前 URL 的主机名。

   www.itheima.com

pathname 设置或返回当前 URL 的路径部分。

   /a/b/c/demo.html

port 设置或返回当前 URL 的端口号。

  80

protocol 设置或返回当前 URL 的协议。

  http

search 设置或返回从问号 (?) 开始的 URL(查询部分)。

  username=jack&password=1234

 

href 设置或返回完整的 URL

  location.href= ...;  指定请求路径

 

3.4.2 方法

assign() 加载新的文档。 将打开新的页面,历史记录也是新。

replace() 用新的文档替换当前文档。将打开新的页面,历史记录将被覆盖。

reload() 重新加载当前文档。 (刷新)

 

DOM

4.1 介绍

l DOMDocument object Model 文档对象模型,用于修改文档(及可以对文档中内容进行CRUD

文档:结构化文档。例如:htmlxml

l DOM规范:html domxml dom

l 之后的操作,将html页面加载内容(浏览器),浏览器将整个文档生成一个对象 document,将采用DOM树方式对html内容进行展现。

4.2 document获取

l 获取方式:window.document ,浏览器将文档加载成功之后,自动生成dom树,并将封装好的对象赋值给window.document

l window 对象属性,可以直接访问,及document. 操作

4.3 Element获取

document.getElementById("id属性的值")  <xxx id=""> 通过标签id属性值 获得 元素

id属性值,必须在整个文档唯一的。

document.getElementsByName("name属性值")  <xxx name=""> 通过标签name属性获得元素们

name属性值可以重复的。

<input type="radio" name="gender" value="">

<input type="radio" name="gender" value="">

document.getElementsByClassName("class 属性值")  <xxx class="css样式">通过标签class属性获得元素们

document.getElementsByTagName("标签名称")  <xxx>  通过标签名获得 元素们

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
	<script type="text/javascript">
	</script>
</head>
<body>
	<form id="form1" name="myForm" method="get" class="formClass" action="/demo1Servlet"></form>
	<form id="form2" name="myForm" method="post" class="formClass" action="/demo2Servlet"></form>
	<form id="form3" name="myForm" method="post"></form>
	<script type="text/javascript">
		//id , 元素属性值快速获取 , 格式: 对象.属性
		var formObj1 = document.getElementById("form1");
		//alert(formObj1.action);
		
		//name 获得数组
		var forms2 = document.getElementsByName("myForm");
		//alert(forms2.length);
		
		//class
		var forms3 = document.getElementsByClassName("formClass");
		//alert(forms3.length);
		
		// tag
		var forms4 = document.getElementsByTagName("form");
		//alert(forms4.length);
	</script>
</body>
</html>


4.4 Element方法

appendChild() 指定的元素内追加

insertBefore() 指定的元素前追加

elementNode.insertBefore(new_node,existing_node)

new_node 当前新创建element

existing_node 谁之前(已经存在)

removeChild() 删除指定的元素

elementNode.removeChild(node)  父亲吧孩子删除掉。

elementNode 必须是父元素

node 子元素

通过孩子如何父元素

childElement.parent.removeChild(childElement)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>

	<script type="text/javascript">
		//表示 页面加载成功之后,设置函数, 相当于  <body οnlοad="xxx()">
		window.onload = function(){
			//1 创建div
			var newDivObj = document.createElement("div");
			//1.1 设置属性
			newDivObj.setAttribute("id","newDivId");
			//1.2 设置样式
			newDivObj.style.height = "200px";
			newDivObj.style.width = "200px";
			newDivObj.style.border = "1px solid #000";
			// 1.3 内容
			newDivObj.innerHTML = "我在这里傻傻看着你";
			
			//2 获得body document.getElementsByTagName("body")[0]
			// * 简化版 document.body
			
			
			
			
			//3 追加
			//document.getElementsByTagName("body")[0].appendChild(newDivObj);
			document.body.appendChild(newDivObj);

		}
		</script>
</head>
<body>
</body>
</html>
后面继续更新html dom解析及xml dom解析!!!!!!!!!!!!

 

 


 

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值