1、表单提交的拦截
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>表单提交的拦截</title>
<script>
function fun1() {
var username = document.getElementById("username").value;
if(username != null && username != '') {
alert(true);
return true;
} else {
alert(false);
return false;
}
}
window.onload = function() {
document.getElementById("form1").onsubmit = function() {
return false;
}
}
</script>
</head>
<body>
<form action="#" onsubmit="return fun1();" >
<input type=text id=username name="username"/>
<input type=submit value="提交"/>
</form>
<form id="form1" action="#" >
<input type=text id=username name="username"/>
<input type=submit value="提交"/>
</form>
</body>
</html>
2、this
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>this</title>
<script>
console.log(this);
window.onload = function() {
console.log(this);
var arr = document.getElementsByClassName("cls1");
for (var i = 0; i < arr.length; i ++) {
arr[i].onclick = function() {
console.log(this.innerText);
}
}
}
function fun1(obj) {
console.log(obj.innerText);
}
</script>
</head>
<body>
<input type="text"/><br>
<button onclick="fun1(this)" class="cls1">1</button>
<button onclick="fun1(this)" class="cls1">2</button>
<button onclick="fun1(this)" class="cls1">3</button>
<button onclick="fun1(this)" class="cls1">4</button>
<button onclick="fun1(this)" class="cls1">5</button>
<button onclick="fun1(this)" class="cls1">6</button>
</body>
</html>
3、案例1:注册验证
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>注册验证</title>
<script>
window.onload = function() {
usernameInput = document.getElementById("username");
usernameInput.onblur = checkUsername;
passwordInput = document.getElementById("password");
passwordInput.onblur = checkPassword;
}
function checkUsername() {
var reg = /^[a-zA-Z]\w{5,17}$/;
var username = usernameInput.value;
var spanMsg1 = document.getElementById("msg1");
if (!reg.test(username)){
spanMsg1.innerHTML = "<font color='red'>格式错误</font>";
return false;
} else {
spanMsg1.innerHTML = '';
return true;
}
}
function checkPassword() {
var reg = /^.{6,16}$/;
var password = passwordInput.value;
var spanMsg2 = document.getElementById("msg2");
if (!reg.test(password)){
spanMsg2.innerHTML = "<font color='red'>格式错误</font>";
return false;
} else {
spanMsg2.innerHTML = '';
return true
}
}
function check() {
return checkUsername() && checkPassword()
}
</script>
</head>
<body>
<form onsubmit="return check();" action=# >
<label for="username">用户名: </label>
<input type="text" id="username" name="username"/>
<span id="msg1" > </span>
<br>
<label for="password">密码: </label>
<input type="password" id="password" name="password"/>
<span id="msg2" > </span>
<br>
<input type="submit" value="注册"/>
</form>
</body>
</html>
4、案例2:轮播图
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>轮播图</title>
<script>
onload = function() {
startLunbo();
}
var imgArr = [ "../img/small01.jpg",
"../img/small02.jpg",
"../img/small03.jpg"];
var i = 0;
function lunbo() {
var img = document.getElementById("img");
img.src = imgArr[i++%3];
}
function stopLunbo() {
clearInterval(intervalId);
}
function startLunbo() {
intervalId = setInterval("lunbo()", 3000);
}
</script>
</head>
<body>
<img id="img" src="../img/small01.jpg"
onmouseout="startLunbo()"
onmouseover="stopLunbo()" />
</body>
</html>
5、案例3:定时弹广告
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>定时弹广告</title>
<style type="text/css">
#header {
height: 51px;
}
#header div {
height: 100%;
width: 33%;
float: left;
}
#header div li {
float: left;
list-style-type: none;
margin-left: 20px;
}
a {
text-decoration: none;
color: #3286BB;
}
#nav {
height: 50px;
background-color: #222222;
border-radius: 5px;
}
#nav ul {
padding-left: 22px;
padding-top: 12px;
margin-top: 0px;
}
#nav li {
list-style-type: none;
margin-right: 25px;
vertical-align: middle;
display: inline-block;
}
#nav li:first-child {
font-size: 19px;
}
#nav li a {
color: #9D9D9D;
}
#img {
height: 631px;
margin-top: 20px;
}
</style>
<script>
onload = function() {
setTimeout("showAd()", 5000);
}
function showAd() {
var ad = document.getElementById("ad");
ad.style.display = "block";
setTimeout("hideAd()", 5000);
}
function hideAd() {
var ad = document.getElementById("ad");
ad.style.display = "none";
}
</script>
</head>
<body>
<img id=ad src="../img/ad_1.jpg" width="100%" style="display: none;" />
<div id="header">
<div><img src="../img/logo2.png"/></div>
<div><img src="../img/header.png"/></div>
<div>
<ul>
<li><a href="#">登录</a></li>
<li><a href="#">注册</a></li>
<li><a href="#">购物车</a></li>
</ul>
</div>
</div>
<div id="nav">
<ul>
<li><a href=#>首页</a></li>
<li><a href=#>手机电脑</a></li>
<li><a href=#>皮鞋箱包</a></li>
<li><a href=#>母婴用品</a></li>
<li><a href=#>美妆护肤</a></li>
</ul>
</div>
<div id="img">
<img src="../img/1.jpg" width="100%"/>
</div>
</body>
</html>