01.添加事件
02.onload函数
03.选项卡
04.定时器
05.获取非行内样式
06.BOM操作
07.select下拉框和oninput事件
01.添加事件
- 添加事件方式
<div style="width:200px; height:200px; background-color:red" onclick="alert('这是一个div')"></div>
<div style="width:200px; height:200px; background-color:blue" onclick="test()"></div>
<div id="div" style="width:200px; height:200px; background-color:green"></div>
</body>
</html>
<script>
function test() {
console.log('花田里犯了错,说好,破晓前忘掉')
}
var odiv = document.getElementById('div')
odiv.onclick = function () {
console.log('遥远的东方有一条龙')
}
- 显示隐藏图片
操作div的display属性,在block和none之间切换即可
div class="tu">
<img src="feng.jpg" alt="可爱的凤姐"">
</div>
<button id="btn">让凤姐消失</button>
<button id="btn1" onclick="show()">说你爱我</button>
</body>
</html>
<script>
// 显示和隐藏图片操作的是div的display属性,在block和none之间切换
var obutton = document.getElementById('btn')
obutton.onclick = function () {
// 找到指定div,将其display属性修改为none
var odiv = document.getElementsByClassName('tu')[0]
odiv.style.display = 'none'
}
function show() {
var odiv = document.getElementsByClassName('tu')[0]
odiv.style.display = 'block'
}
- this使用
在匿名函数中的this就是当前对象
在onclick=demo(this) 就是当前节点
修改内容
this.innerHTML = 'xxx'
<body>
<div class="tang" style="width:200px; height:200px; background-color:red"></div>
<div class="tang" style="width:200px; height:200px; background-color:blue" onclick="demo(this)"></div>
</body>
</html>
<script>
// 点击div,将宽度有200px修改为400px
var odiv = document.getElementsByClassName('tang')[0]
odiv.onclick = function () {
console.log(this)
// this就是odiv
this.style.width = '400px'
// 给div添加内容
this.innerHTML = '秦时明月汉时关,万里长征人未还,但使龙城飞将在,不教胡马度阴山'
}
function demo(obj) {
// 这里面的this是window,在js里面写的所有的函数都是window的函数,调用demo其实就是 window.demo()
console.log(this)
// var odiv = document.getElementsByClassName('tang')[1]
obj.style.height = '400px'
}
点击之前:
点击之后:
- 切换背景色
<body>
<div id="bai" style="width:300px; height:300px; background-color:red;"></div>
</body>
</html>
<script>
var odiv = document.getElementById('bai')
odiv.onclick = function () {
// 先获取div的背景色
color = this.style.backgroundColor
if (color == 'red') {
this.style.backgroundColor = 'yellow'
} else {
this.style.backgroundColor = 'red'
}
}
表单内容控制
方法一:this传值
<body>
<input id="ip" type="text" value="请输入用户名">
<input type="text" value="请输入用户名" onfocus="cleara(this)" onblur="recover(this)">
</body>
</html>
<script>
// clear是关键字,不能使用函数名
function cleara(obj) {
// console.log('clear')
if (obj.value == "请输入用户名") {
obj.value = ''
}
}
function recover(obj) {
if (obj.value == '') {
obj.value = '请输入用户名'
}
}
方法二:getElementById()
var oinput = document.getElementById('ip')
oinput.onfocus = function () {
// 判断要不要清空
if (this.value == "请输入用户名") {
this.value = ''
}
}
oinput.onblur = function () {
// 判断内容是不是为空,如果为空变成下面这个
if (this.value == '') {
this.value = '请输入用户名'
}
}
鼠标点击输入框后:
02.onload函数
window的事件,windows.onload = function () {} 是在整个文档加载完毕之后执行,但是自己写的onclick的点击函数不能写到onload里面,因为内部函数只能在内部使用,不能再外部使用
<html>
<head>
<meta charset="UTF-8">
<title>onload函数</title>
<script type="text/javascript">
// demo() 有错
window.onload=function(){
var odiv = document.getElementById('div2')
odiv.onclick = function () {
this.style.backgroundColor = 'red'
}
}
// function demo(obj) {
// obj.style.backgroundColor = 'cyan'
// }
</script>
</head>
<body>
<div id="div2" style="width:300px; height:300px; background-color:pink" onclick="demo(this)">
</div>
</body>
</html>
<script type="text/javascript">
// function demo(obj){
// obj.style.backgroundColor='red'
// }
</script>
03.选项卡
<style>
button {
width: 200px;
height: 100px;
font-size: 40px;
background-color: pink;
margin-left: 50px;
display: inline-block;
}
div {
width: 970px;
height: 600px;
font-size: 50px;
background-color: yellow;
margin-left: 50px;
margin-top: 50px;
display: none;
}
.active {
font-size: 50px;
background-color: blue;
}
.show {
display: block;
}
</style>
</head>
<body>
<button class="active" onclick="dudu(this, 0)">周杰伦</button>
<button onclick="dudu(this, 1)">王力宏</button>
<button onclick="dudu(this, 2)">张学友</button>
<button onclick="dudu(this, 3)">刘德华</button>
<div class="show">菊花台、千里之外、七里香、霍元甲、听妈妈的话、稻香、双节棍、简单爱</div>
<div>花田错、龙的传人、唯一</div>
<div>慢慢、吻别、一千个伤心的理由</div>
<div>谢谢你的爱、冰雨、天意、忘情水</div>
</body>
</html>
<script>
// 得到所有的button
var abuttons = document.getElementsByTagName('button')
// 得到所有的div
var adivs = document.getElementsByTagName('div')
function dudu(obj, index) {
// 先将所有的button的class属性设置为空
for (var i = 0; i < abuttons.length; i++) {
abuttons[i].className = ''
adivs[i].className = ''
}
// 给指定的button添加样式
obj.className = 'active'
// 给指定的div添加样式
adivs[index].className = 'show'
}
</script>
04.定时器
定时器:分为两种,一种是周期性定时器,一种是一次性定时器
周期性:每隔5s执行一次函数
一次性:几秒之后执行一次函数,执行完毕定时器结束
var timer = setTimeout(fn, 5000)
5000ms之后执行fn一次。然后结束
销毁定时器 clearTimeout(timer)
- 一次性计时器
</head>
<body>
<div id="baby">
<img height="800px" src="meinv.jpg" alt="气质美女">
</div>
<button id="btn">点我给你福利</button>
</body>
</html>
<script>
var odiv = document.getElementById('baby')
var obtn = document.getElementById('btn')
// timer就是一个定时器
var timer = setTimeout(function () {
odiv.style.display = 'none'
}, 5000)
obtn.onclick = function () {
// 清除timer这个定时器
clearTimeout(timer)
}
</script>
- 周期计时器
</head>
<body>
<button onclick="demo()">一行司机上青天</button>
</body>
</html>
<script>
var timer = setInterval(function () {
console.log('两个女孩鸣翠柳')
}, 5000)
function demo() {
clearInterval(timer)
}
</script>
05.获取非行内样式
- IE浏览器获取非行内样式方式
obj.currentStyle['name']
- 火狐和谷歌获取方式
getComputedStyle(odiv, null)['width']
- 获取非行内样式的兼容性写法
function getStyle(obj, name) {
return obj.currentStyle ? obj.currentStyle[name] : getComputedStyle(obj, null)[name]
}
<style>
div {
width: 300px;
height: 300px;
background-color: pink;
}
</style>
</head>
<body>
<div id="lala"></div>
<input type="text" id="ip">
<button onclick="demo()">点我获取div宽度</button>
</body>
</html>
<script>
var odiv = document.getElementById('lala')
function demo() {
// 获取div的宽度,只能获取行内样式
var kuan = odiv.style.width
// 获取非行内样式
var kuan = odiv.currentStyle['width']
var kuan = getComputedStyle(odiv, null)['width']
var kuan = getStyle(odiv, 'width')
// 显示到input框中
var oinput = document.getElementById('ip')
oinput.value = kuan
}
06.BOM操作
<body>
<button id="btn">点我</button>
<button id="btn1">摸我</button>
<button id="btn2">张家辉</button>
<button id="btn3">张家辉</button>
</body>
</html>
<script>
var obutton = document.getElementById('btn')
var obutton1 = document.getElementById('btn1')
var obutton2 = document.getElementById('btn2')
var obutton3 = document.getElementById('btn3')
obutton.onclick = function () {
ret = window.confirm('今天晚上准备吃什么')
if (ret == true) {
console.log('你点击了确定')
} else {
console.log('你点击了取消')
}
}
obutton1.onclick = function () {
window.open('http://www.baidu.com/', '_self')
}
obutton2.onclick = function () {
// window.history.go(2)
window.history.back()
// file:///C:/Users/ZBLi/Desktop/1805/day02/14-bom.html
}
obutton3.onclick = function () {
// 得到当前的url
// console.log(location.href)
// location.href = 'http://www.baidu.com/'
location.reload()
}
</script>
07.select下拉框和oninput事件
onchange : 事件,用户点击下拉框触发
selectedIndex : 用户点击的option的下标,下标从0开始
options : osel.options 可以得到所有的option对象,这是一个数组
input框的oninput事件,只要内容改变,就会触发
<body>
<select name="刺客" id="sel">
<option value="1">阿珂</option>
<option value="2">兰陵王</option>
<option value="3">孙悟空</option>
<option value="4">赵云</option>
<option value="5">李白</option>
</select>
<input type="text" id="ip">
</body>
</html>
<script>
var osel = document.getElementById('sel')
osel.onchange = function () {
// alert('我被出发了')
// alert(osel.selectedIndex)
alert(osel.options[osel.selectedIndex].innerHTML)
}
var oinput = document.getElementById('ip')
oinput.oninput = function () {
console.log(this.value)
}
</script>