(2020-09-09)javascript_8

1.javascript browser bom

bom-----浏览器对象模型

1.1javascript  window  对象

所有浏览器都支持wundow对象。它代表浏览器的窗口。

所有全局javascript对象,函数和变量自动成为window对象的成员。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script>
			window.onload=function(){
				document.getElementById("but").onclick=function(){
					alert("按钮点击事件");
				}
			}
		</script>
	</head>
	<body>
		<input id="but" type="button" value="测试按钮的点击事件" />
	</body>
</html>

window.open()----打开新窗口

window.open(url,name,specs,replace)

url----------可选。打开指定的页面的url。如果没有指定url,打开一个新的空白窗口

name-------可选。指定target属性或窗口的名称。支持以下值:

                  _blank-url  加载到一个新的窗口。这是默认

                  _parent-url  加载到父框架

                  _self-url   替换当前页面

                  _top-url    替换任何可加载的框架集

                   name-窗口名称

specs---------可选。一个逗号分隔的项目列表。支持以下值:

            channelmode=yes|no|1|0--------是否要在影院模式显示 window。默认是没有的。仅限IE浏览器

              directories=yes|no|1|0-----------是否添加目录按钮。默认是肯定的。仅限IE浏览器

              fullscreen=yes|no|1|0------------浏览器是否显示全屏模式。默认是没有的。在全屏模式下的 window还必须在影院模式。                 仅限IE浏览器。

              height=pixels--------------窗口的高度。最小.值为100

             left=pixels------------该窗口的左侧位置

              location=yes|no|1|0-------是否显示地址字段.默认值是yes

               menubar=yes|no|1|0-------是否显示菜单栏.默认值是yes

               resizable=yes|no|1|0-------是否可调整窗口大小.默认值是yes

               scrollbars=yes|no|1|0-------是否显示滚动条.默认值是yes

               status=yes|no|1|0---------是否要添加一个状态栏.默认值是yes

                titlebar=yes|no|1|0-------是否显示标题栏.被忽略,除非调用HTML应用程序或一个

                 值得信赖的对话框.默认值是yes。

                toolbar=yes|no|1|0----------是否显示浏览器工具栏.默认值是yes

                 top=pixels----------------窗口顶部的位置.仅限IE浏览器

                 width=pixels-------------窗口的宽度.最小.值为100

replace--------Optional.Specifies规定了装载到窗口的url是在窗口的浏览历史中创建一个新条目,还是替换浏览历史中的

                    当true-url   替换浏览历史中的当前条目。

                    false -url      在浏览历史中创建新的条目。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script>
			window.onload=function(){
				document.getElementById("but").onclick=function(){
					//打开一个新窗口
					//window.open(); //空白窗口
					//window.open("https://www.baidu.com"); //打开百度对应的窗口
					//window.open("https://www.baidu.com","_self");
					window.open("https://www.baidu.com","_blank","width=200,height=200",false);
				}
			}
		</script>
	</head>
	<body>
		<input id="but" type="button" value="测试按钮的点击事件" />
	</body>
</html>

•	window.close() - 关闭当前窗口
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script>
			window.onload=function(){
				document.getElementById("but1").onclick=function(){
					//打开一个新窗口
					window.open("https://www.baidu.com","_blank","width=200,height=200",false);	
				}
				document.getElementById("but2").onclick=function(){
					//关闭当前窗口
					window.close();
				}
			}
		</script>
	</head>
	<body>
		<input id="but1" type="button" value="打开新窗口" /><br>
		<input id="but2" type="button" value="关闭打开的新窗口" /><br>
	</body>
</html>

alert--------------显示带有一段消息和一个确认按钮的警告框。

confirm-----------显示带有一段消息以及确认按钮和取消按钮的对话框。

Location对象

search------------返回一个url的查询部分

History对象

back()---------加载history列表中的前一个url

forward()---------加载history列表中的下一个url

go()--------------加载history列表中的某个具体页面

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script>
			window.onload=function(){
				document.getElementById("but5").onclick=function(){
					window.open("javascript2.html?name=zhangsan&age=23","_self");
				}
			}
		</script>
	</head>
	<body>
		<input id="but5" type="button" value="测试Location的search属性" /><br>
		<a href="javascript2.html?name=zhangsan&age=23">跳转到javascript2.html</a>
	</body>
</html>


<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script>
			window.onload=function(){
				var testvalu=window.location.search;
				//testvalu--->?name=zhangsan&age=23
				var strarray=testvalu.split("&");
				var info="";
				for(var i=0;i<strarray.length;i++){
					info=info+strarray[i].split("=")[1]+",";
				}
				info=info.slice(0,info.length-1);
				document.getElementsByTagName("h2")[0].innerHTML=info;
				
				document.getElementById("but1").onclick=function(){
					//window.history.back();
					window.history.go(-1);
				}
			}
		</script>
	</head>
	<body>
		<h1>测试Location的search属性</h1>
		<h2></h2>
		<input id="but1" type="button" value="测试History 的back()" /><br>
	</body>
</html>

2.JSON简介

 json:javascript   object   notation(javascript  对象标记法)。

json  是一种存储和交互数据的语法【文本形式】。

交换数据

当数据在浏览器与服务器之间进行交换时,这些数据只能是文本。

json属于文本,并且我们能够把任何javascript对象转换为json,然后将json发送到服务器。

我们也能把从服务器接收到的任何json转换为javascript对象。

json数据的组成

1.json对象

通过{}包围,在里面提供键值对数据形式【键:值】,可以出现多个,中间用“,”分隔。”

举例:

{ name:"Bill Gates", age:62, city:"Seattle" } 

2.json数组

通过“【】”包围,在里面提供json对象

举例:

[{ name:"Bill Gates", age:62, city:"Seattle" } ,
{ name:"Bill Gates", age:62, city:"Seattle" } ]

通常我们所接触json数据都是json对象中包含json数组,json数组包含json对象

0http://v.juhe.cn/weather/index?format=2&cityname=天津&key=1323523
{ "resultcode": "200", "reason": "查询成功!", "result": { "sk": { /*当前实况天气*/ "temp": "21", /*当前温度*/ "wind_direction": "西风", /*当前风向*/ "wind_strength": "2级", /*当前风力*/ "humidity": "4%", /*当前湿度*/ "time": "14:25" /*更新时间*/ }, "today": { "city": "天津", "date_y": "2014年03月21日", "week": "星期五", "temperature": "8℃~20℃", /*今日温度*/ "weather": "晴转霾", /*今日天气*/ "weather_id": { /*天气唯一标识*/ "fa": "00", /*天气标识00:晴*/ "fb": "53" /*天气标识53:霾 如果fa不等于fb,说明是组合天气*/ }, "wind": "西南风微风", "dressing_index": "较冷", /*穿衣指数*/ "dressing_advice": "建议着大衣、呢外套加毛衣、卫衣等服装。", /*穿衣建议*/ "uv_index": "中等", /*紫外线强度*/ "comfort_index": "",/*舒适度指数*/ "wash_index": "较适宜", /*洗车指数*/ "travel_index": "适宜", /*旅游指数*/ "exercise_index": "较适宜", /*晨练指数*/ "drying_index": ""/*干燥指数*/ }, "future": [ /*未来几天天气*/ { "temperature": "28℃~36℃", "weather": "晴转多云", "weather_id": { "fa": "00", "fb": "01" }, "wind": "南风3-4级", "week": "星期一", "date": "20140804" }, { "temperature": "28℃~36℃", "weather": "晴转多云", "weather_id": { "fa": "00", "fb": "01" }, "wind": "东南风3-4级", "week": "星期二", "date": "20140805" }, { "temperature": "27℃~35℃", "weather": "晴转多云", "weather_id": { "fa": "00", "fb": "01" }, "wind": "东南风3-4级", "week": "星期三", "date": "20140806" }, { "temperature": "27℃~34℃", "weather": "多云", "weather_id": { "fa": "01", "fb": "01" }, "wind": "东南风3-4级", "week": "星期四", "date": "20140807" }, { "temperature": "27℃~33℃", "weather": "多云", "weather_id": { "fa": "01", "fb": "01" }, "wind": "东北风4-5级", "week": "星期五", "date": "20140808" }, { "temperature": "26℃~33℃", "weather": "多云", "weather_id": { "fa": "01", "fb": "01" }, "wind": "北风4-5级", "week": "星期六", "date": "20140809" }, { "temperature": "26℃~33℃", "weather": "多云", "weather_id": { "fa": "01", "fb": "01" }, "wind": "北风4-5级", "week": "星期日", "date": "20140810" } ] }, "error_code": 0 }

我们通常要在提交/下载json数据之前/以后,要对json数据进行转换

1.json字符串抓换成json对象

2.json对象转换成json字符串

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script>
			window.onload=function(){
				document.getElementById("but1").onclick=function(){
					//创建一个json对象
					var myObj = { name:"Bill Gates",  age:62, city:"Seattle" };
					alert(myObj);
					//将json对象转换成json字符串
					var jsonstring=JSON.stringify(myObj);
					alert(jsonstring);
				}
				document.getElementById("but2").onclick=function(){
					//创建json字符串
					var mystring ="{name:'Bill Gates', age:62,city:'Seattle'}";
					alert(mystring);
					var jsonobj = eval('(' + mystring + ')');
					alert(jsonobj.name+","+jsonobj.age+","+jsonobj.city);
				}
			}
		</script>
	</head>
	<body>
		<input  id="but1" type="button" value="json对象转换json字符串" /><br>
		<input  id="but2" type="button" value="json字符串转换json对象" /></br>
	</body>
</html>

与后台交互,前端代码示例:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<!--导入表jquery-->
		<script src="jquery-3.4.1.js"></script>
		<script>
			$(function(){
				$("#but1").click(function(){
					$.get("http://127.0.0.1:8080/testajax/getStudentAll",
					function(data){
						//var jsonobj = eval('(' + data + ')');
						var trvalue="";
						for(var i=0;i<data.length;i++){
							 trvalue=trvalue+"<tr><td>"+data[i].stuid+
							 				"</td><td>"+data[i].stuname+
							 				"</td><td>"+data[i].stuage+
							 				"</td><td>"+data[i].stuaddress+"</td></tr>";
						}
						document.getElementById("main").innerHTML=trvalue;
					},"json");
				});
			});
		</script>
	</head>
	<body>
		<center>
			<input id="but1" type="button" value="得到学生信息"/>
			<table id="main" border="1px"></table>
		</center>
	</body>
</html>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值