Javascript中的dom对象
- 控制html元素
- 控制html元素的属性
- 控制css
-
控制事件
JS的常用事件-注册事件的两种方式
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS的常用事件</title>
</head>
<body>
<script type="text/javascript">
/*
JS中的事件
blur失去焦点
focus获得焦点
click鼠标单击
dblclick鼠标双击
keydown键盘按下
keyup键盘弹起
mousedown鼠标按下
mouseover鼠标经过
mousemove鼠标移动
mouseout鼠标离开
mouseup鼠标弹起
reset表单重置
submit表单提交
change下拉列表选中项改变,或文本框内容改变
select文本被选定
load页面加载完毕(整个HTML页面中所有的元素全部加载完毕之后发生)
任何一个事件都会对应一个事件句柄,事件句柄是在事件前添加on
onXXX这个事件句柄出现在一个标签的属性位置上(事件句柄以属性的形式存在)
*/
//对于当前程序来说,sayHello函数被称为回调函数(callback函数)
//回调函数的特点:自己把这个函数代码写出来了。但是这个函数不是自己负责调用,有其他程序负责调用该函数
function sayHello(){
alert("hello,java");
}
</script>
<!-- 注册事件的第一种方式,直接在标签中使用事件句柄 -->
<!-- 一下代码的含义是:将sayHello函数注册到按钮上,等待click事件发生之后,该函数
被浏览器调用,我们称这个函数为回调函数 -->
<input type="button" value="hello" onclick="sayHello()" />
<input type="button" value="hello2" id="mybtn" />
<input type="button" id="mybtn1" value="hello3" />
<input type="button" id="mybtn2" value="hello4" />
<script type="text/javascript">
function doSome(){
alert("do some!");
}
/*
第二种注册事件的方式,是使用纯JS代码完成事件的注册
*/
//第一步:先获取这个按钮对象(document是全部小写,内置对象,可以直接使用,document就代表整个HTML页面)
var btnObj = document.getElementById("mybtn");
//第二步:给按钮对象的onclick属性赋值
btnObj.onclick=doSome;// 注意千万别加小括号,btnObj.onclick = doSome();这是错误的写法,这行代码的含义是,将回调函数doSome注册到click事件上
var mybtn1 = document.getElementById("mybtn1");
mybtn1.onclick = function (){//这个函数没有名字,叫做匿名函数,这个匿名函数也是一个回调函数
alert("test。。。。");//这个函数在页面打开的时候注册,不会被调用,在click事件发生以后才会调用
}
document.getElementById("mybtn2").onclick = function(){
alert("test2222222222");
}
</script>
</body>
</html>
1.按钮点击事件 onclick
具体用法:
<script>
function 点击事件的处理函数(){
//事件处理动作
}
</script>
<input type=”button” value=”按钮” οnclick=”按钮点击以后的处理函数”/>
2.页面初始化事件 onload
具体用法:
<script>
function 处理函数(){
//事件处理动作
}
</script>
<body οnlοad=”处理函数”></body>
3.常见的javascript事件,事件的具体使用方法
1.页面初始化事件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<script type="text/javascript">
// function testonload(){
// alert("测试页面初始化成功")
// }
window.onload=function(){
alert("测试页面初始化成功");
}
</script>
</head>
<!-- <body onload="testonload()" > -->
<body>
</body>
</html>
2.按钮点击事件 onclick
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<script type="text/javascript">
function testclick(){
alert("按钮点击事件1");
}
window.onload=function(){
var but1=document.getElementById("but1");
but1.onclick=function(){
alert("按钮点击事件2");
}
}
</script>
</head>
<body>
<input type="button"onclick="testclick()" name="" id="" value="测试按钮点击事件1" /><br>
<input type="button" name="" id="but1" value="测试按钮点击事件2" />
</body>
</html>
3.onchange 事件,当用户改变输入字段的内容时触发
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<script type="text/javascript">
function test1(){
alert("输入内容被改变时触发");
}
window.onload=function(){
var text1=document.getElementById("text1");
text1.onchange=function(){
alert("输入内容被改变时触发2");
}
}
</script>
</head>
<body>
<input type="text"onchange="test1()" name="" id="" value="hello" /><br>
<input type="text" name="" id="text1" value="hello" />
</body>
</html>
5.onfocus当获得焦点时触发
6.onblur当失去焦点时触发
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<script type="text/javascript">
function testonfocus(){
var text1=document.getElementById("text1");
text1.value="";
}
window.onload=function(){
var text1=document.getElementById("text1");
text1.onblur=function(){
var val=text1.value;
var rgel=new RegExp(/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/);
var bool=rgel.test(val);
if(bool){
alert("输入正确");
}else{
alert("输入错误请重新输入");
}
}
}
</script>
</head>
<body>
<input onfocus="testonfocus()" type="text" name="" id="text1" value="请输入手机号码" />
</body>
</html>
6.onmouseover 和 onmouseout 事件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<script type="text/javascript">
window.onload=function(){
var imgl=document.getElementById("img1");
imgl.onmouseover=function(){
imgl.style.width='500px';
imgl.style.height='500px';
}
imgl.onmouseout=function(){
imgl.style.width='200px';
imgl.style.height='200px';
}
}
</script>
</head>
<body>
<img id="img1" src="imgs/22.jpg" >
</body>
</html>
7.onsubmit 事件会在表单中的确认按钮【submit】被点击时发生。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<script type="text/javascript">
function testonsubmit(){
var text1=document.getElementById("text1");
var span1=document.getElementById("span1");
var pass1=document.getElementById("password");
var username=text1.value;
var password=pass1.value;
if(username=="zhangsan"&&password=="123456"){
alert("登陆成功");
return true;
}else {
span1.innerHTML="<font color='red' size='7'>用户名密码输入错误</font>"
return false;
}
}
</script>
</head>
<body>
<span id="span1">
</span><br>
<form action="#" method="get" onsubmit="return testonsubmit()" >
用户名:<input type="text" name="username" id="text1" value="" /><br>
密码:<input type="password" name="password1" id="password" value="" /><br>
<input type="button" name="" id="" value="普通按钮" /><br>
<input type="submit" name="" id="" value="登录" />
</form>
</body>
</html>
8.onkeydown 事件会在用户按下一个键盘按键时发生。
JS代码捕捉回车键
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS代码捕捉回车键</title>
</head>
<body>
<script type="text/javascript">
window.onload = function(){
var usernameElt = document.getElementById("username");
// 回车键的键值是 13
// ESC键的键值是 27
/*
usernameElt.onkeydown = function(a,b,c){
//获取键值
alert(a);// [object KeyboardEvent]
alert(b);
alert(c);
}
*/
/*
usernameElt.onkeydown = function(){
//获取键值
//对于“键盘事件对象”来说,都有keyCode属性来获取键值
alert(event.keyCode);
}
}
*/
// usernameElt.onkeydown = function(event){
// if(event.keyCode === 13){
// alert("正在进行验证....");
// }
// }
// }
/*
usernameElt.onkeydown = function(a){
if(a.keyCode === 13){
alert("正在验证....");
}
}
*/
usernameElt.onkeydown = function(){
if(event.keyCode === 13){
alert("正在验证....");
}
}
//张三程序员写了这样一个函数
// function sum(){
// alert("sum execute...!");
// }
function sum(a){
// alert("sum execute...!");
alert(a);
}
//李四程序员调用sum函数
// sum();
sum("hello world!");
}
</script>
<input type="text" id="username" />
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<script type="text/javascript">
function test1(){
var num=event.keyCode;
if(num==32){
alert("暂停");
}
if(num==65||num==37){
alert("向左移动");
}
if(num==68||num==39){
alert("向右移动");
}
if(num==87||num==38){
alert("向上移动");
}
if(num==83||num==40){
alert("向下移动");
}
}
</script>
</head>
<body onkeydown="test1(event)">
</body>
</html>
图像的加载被中断。 | |
元素失去焦点。 | |
域的内容被改变。 | |
当用户点击某个对象时调用的事件句柄。 | |
当用户双击某个对象时调用的事件句柄。 | |
在加载文档或图像时发生错误。 | |
元素获得焦点。 | |
某个键盘按键被按下。 | |
某个键盘按键被按下并松开。 | |
某个键盘按键被松开。 | |
一张页面或一幅图像完成加载。 | |
鼠标按钮被按下。 | |
鼠标被移动。 | |
鼠标从某元素移开。 | |
鼠标移到某元素之上。 | |
鼠标按键被松开。 | |
重置按钮被点击。 | |
窗口或框架被重新调整大小。 | |
文本被选中。 | |
确认按钮被点击。 | |
用户退出页面。 |
创建新的 HTML 元素
document.createElement("元素名称");
document.createTextNode("文本内容");
父元素的dom对象.appendChild(node);
删除元素 父元素的dom对象.removeChild(子元素的dom对象);
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
#div1{background: url(imgs/25.jpg);
background-repeat: no-repeat;
background-size:cover;
background-attachment: fixed;
background-color: #CCCCCC;
width: 500px;
height: 500px;
}
</style>
<script type="text/javascript">
window.onload=function(){
var addbut=document.getElementById("add");
var deletebut=document.getElementById("delete");
addbut.onclick=function(){
var htext=document.createTextNode("这是我创建的第一个元素");
var hdom=document.createElement("h1");
hdom.appendChild(htext);
var divdom=document.getElementById("div1");
divdom.appendChild(hdom);
}
deletebut.onclick=function(){
var divdom=document.getElementById("div1");
var hdom=document.getElementById("h1");
//node.removeChild(node) node Node object 必需。您希望删除的节点对象。
divdom.removeChild(divdom.childNodes[0]);
}
}
</script>
</head>
<body>
<div id="div1">
<h1 id="h1">测试添加移除元素</h1>
</div>
<input type="button" name="" id="add" value="添加元素" /><br>
<input type="button" name="" id="delete" value="删除元素" /><br>
</body>
</html>
JavaScript_10
JavaScript中的BOM对象
浏览器对象模型--Browser ObjectModel (BOM)
Window 对象
属性
有三种方法能够确定浏览器窗口的尺寸(浏览器的视口,不包括工具栏和滚动条)。
对于Internet Explorer、Chrome、Firefox、Opera 以及 Safari:
window.innerHeight - 浏览器窗口的内部高度
window.innerWidth - 浏览器窗口的内部宽度
对于 Internet Explorer 8、7、6、5:
document.documentElement.clientHeight
document.documentElement.clientWidth
或者
document.body.clientHeight
document.body.clientWidth
实用的 JavaScript 方案(涵盖所有浏览器):
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
window.onload=function(){
//确定浏览器窗口的尺寸(浏览器的视口,不包括工具栏和滚动条)
var w=window.innerWidth ||
document.documentElement.clientWidth ||
document.body.clientWidth;
var h=window.innerHeight ||
document.documentElement.clientHeight ||
document.body.clientHeight;
window.alert(w+"*"+h);
}
</script>
</head>
<body>
</body>
</html>
方法:
其他方法:open() 方法用于打开一个新的浏览器窗口或查找一个已命名的窗口
格式:window.open(URL,name,features,replace)
URL | 一个可选的字符串,声明了要在新窗口中显示的文档的 URL。如果省略了这个参数,或者它的值是空字符串,那么新窗口就不会显示任何文档。 |
name | 一个可选的字符串,该字符串是一个由逗号分隔的特征列表,其中包括数字、字母和下划线,该字符声明了新窗口的名称。这个名称可以用作标记 <a> 和 <form> 的属性 target 的值。如果该参数指定了一个已经存在的窗口,那么 open() 方法就不再创建一个新窗口,而只是返回对指定窗口的引用。在这种情况下,features 将被忽略。 |
features | 一个可选的字符串,声明了新窗口要显示的标准浏览器的特征。如果省略该参数,新窗口将具有所有标准特征。 |
replace | 一个可选的布尔值。规定了装载到窗口的 URL 是在窗口的浏览历史中创建一个新条目,还是替换浏览历史中的当前条目。支持下面的值: true - URL 替换浏览历史中的当前条目。 false - URL 在浏览历史中创建新的条目。 |
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
function open_win(){
window.open("https://www.baidu.com/");
}
function open_aboutblank(){
window.open("about:blank","空白页","width=200,height=100",false);
}
</script>
</head>
<body>
<input type=button value="Open百度" onclick="open_win()" /><br>
<input type=button value="about:blank" onclick="open_aboutblank()" />
</body>
</html>
重要事项:请不要混淆方法 Window.open() 与方法 Document.open(),这两者的功能完全不同。为了使您的代码清楚明白,请使用 Window.open(),而不要使用 open()。
close() 方法用于关闭浏览器窗口。
说明:方法 close() 将关闭有 window 指定的顶层浏览器窗口。某个窗口可以通过调用 self.close() 或只调用 close() 来关闭其自身。
只有通过 JavaScript 代码打开的窗口才能够由 JavaScript 代码关闭。这阻止了恶意的脚本终止用户的浏览器。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<script type="text/javascript">
var mywindow = null;
window.onload=function(){
mywindow= window.open("","","");
mywindow.document.write("this is mywindow");
}
function closemywindow(){
mywindow.close();
}
</script>
</head>
<body>
<input onclick="closemywindow()" type="button" name="" id="" value="guanbi" />
</body>
</html>
JavaScript 弹窗方法
在 JavaScript 中创建三种消息框:警告框、确认框、提示框。
警告框:window.alert("sometext");
确认框:window.confirm("sometext");
当确认卡弹出时,用户可以点击 "确认" 或者 "取消" 来确定用户操作。
当你点击 "确认", 确认框返回 true, 如果点击 "取消", 确认框返回 false。
提示框:window.prompt("sometext","defaultvalue");
当提示框出现后,用户需要输入某个值,然后点击确认或取消按钮才能继续操纵。
如果用户点击确认,那么返回值为输入的值。如果用户点击取消,那么返回值为 null。
参数1---提示信息
参数2---提示框的默认值
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
#div1{
width: 300px;
height: 300px;
background-color: red;
}
</style>
<script>
window.onload=function(){
var butObj=document.getElementById("but1");
butObj.onclick=function(){
//window.alert("测试警告框");
var val=window.confirm("确定要删除吗?");
if(val){
var divObj=document.getElementById("div1");
var hObj=document.getElementById("h");
divObj.removeChild(hObj);
}
}
var butObj2=document.getElementById("but2");
butObj2.onclick=function(){
var val=window.prompt("请输入姓名","");
if(val.length>0){
alert(val);
}else{
alert("不能为空!");
}
}
}
</script>
</head>
<body>
<div id="div1">
<h1 id="h">测试确认框</h1>
</div>
<input id="but1" type="button" value="删除H1" /><br>
<input id="but2" type="button" value="测试提示框" />
</body>
</html>
子对象
Window Screen--屏幕
window.screen 对象包含有关用户屏幕的信息。
- 总宽度和总高度 --- screen.width / screen.height
- 可用宽度和可用高度----screen.availWidth / screen.availHeight
- 色彩深度----screen.colorDepth
- 色彩分辨率----screen.pixelDepth
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
//1.总宽度和总高度 --- screen.width / screen.height
window.document.write("<h1>总宽度和总高度:"+window.screen.width+
"*"+window.screen.height+"</h1>");
//2.可用宽度和可用高度----screen.availWidth / screen.availHeight
window.document.write("<h1>可用宽度和可用高度:"+window.screen.availWidth+
"*"+window.screen.availHeight+"</h1>");
//3.色彩深度----screen.colorDepth
window.document.write("<h1>色彩深度:"+window.screen.colorDepth+"</h1>");
//3.色彩分辨率----screen.pixelDepth
window.document.write("<h1>色彩分辨率:"+window.screen.colorDepth+"</h1>");
</script>
</head>
<body>
</body>
</html>
2.Window Location---页面的地址 (URL)
对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面。
location.href 属性返回当前页面的 URL。
location.search 属性是一个可读可写的字符串,可设置或返回当前 URL 的查询部分(问号 ? 之后的部分)。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
document.write("<h1>href:"+window.location.href+"</h1>");
document.write("<h1>pathname :"+window.location.pathname +"</h1>");
document.write("<h1>search :"+window.location.search+"</h1>");
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1><a href="javascript6.html?name=zhangsan">eee</a></h1>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
document.write("<h1>search :"+window.location.search+"</h1>");
window.onload=function(){
var a=document.getElementById("but1");
a.onclick=function(){
window.location.href=("https://www.w3school.com.cn/jsref/prop_loc_search.asp?测试成功");
}
}
</script>
</head>
<body>
<input type="button" name="" id="but1" value="测试window.location.href" />
</body>
</html>