https://www.w3school.com.cn/js/index.asp
1.JavaScript:Browser BOM
BOM--浏览器对象模型
1.1JavaScript window对象
所有浏览器都支持window对象,它代表浏览器的窗口。
所有全局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,relace)
URL | 可选。打开指定的页面的URL。如果没有指定URL,打开一个新的空白窗口 | ||||||||||||||||||||||||||||
name | 可选。指定target属性或窗口的名称。支持以下值: _blank - URL加载到一个新的窗口。这是默认 _parent - URL加载到父框架 _self - URL替换当前页面 _top - URL替换任何可加载的框架集 name - 窗口名称 | ||||||||||||||||||||||||||||
specs | 可选。一个逗号分隔的项目列表。支持以下值:
| ||||||||||||||||||||||||||||
replace | Optional.Specifies规定了装载到窗口的 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.hao123.com");//打开导航对应的窗口
//window.open("https://www.hao123.com","_self);
window.open("https://www.hao123.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.hao123.com","_blank","width=300,height=300",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>
显示带有一段消息和一个确认按钮的警告框。 | |
显示带有一段消息以及确认按钮和取消按钮的对话框。 |
Location对象
返回一个URL的查询部分 |
History对象
加载 history 列表中的前一个 URL | |
加载 history 列表中的下一个 URL | |
加载 history 列表中的某个具体页面 |
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload=function(){
document.getElementById("but5").onclick=function(){
window.open("js2.html?name=lisi&age=27","_self");
}
}
</script>
</head>
<body>
<input id="but5" type="button" value="测试location的search属性" /><br />
<a href="js2.html?name=lisi&age=27">跳转到js2.html</a>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload=function(){
var testvalu=window.location.search;
//testvalu=window.location.search;
var strarray=testvalu.split("&");
varinfo="";
for(var i=0;i<strarray.length;i++){
info=info+strarray[i].split("=")[1]+",";
}
info=info.slice(0,info,length-1);
document.getElementById("but1").onclick=function(){
//window.history.back();
window.history(-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>