一、排他功能
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>按钮排他功能</title>
</head>
<body>
<input type="button" value="没怀孕">
<input type="button" value="没怀孕">
<input type="button" value="没怀孕">
<input type="button" value="没怀孕">
<input type="button" value="没怀孕">
</body>
<script>
var inputObjs=document.getElementsByTagName("input");
//循环遍历所有的按钮
for (var i=0;i<inputObjs.length;i++){
//为每个按钮都要注册点击事件
inputObjs[i].onclick=function () {
//把所有的按钮的value的值都设为默认的值----没怀孕
for (var j=0;j<inputObjs.length;j++){
inputObjs[j].value="没怀孕";
}
//当前被点击的按钮设置为“怀孕了”
// inputObjs[i].value="怀孕了"; 不可以,为什么???
this.value="怀孕了";
}
}
// inputObjs[i].value="怀孕了"; 不可以,为什么???
//第一个for循环是在页面加载的时候,执行完毕了
//事件是在触发的时候执行的
</script>
</html>
二、点击超链接切换图片
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>点击超链接切换图片</title>
</head>
<body>
<a href="images/1.jpg" id="a" style="display:inline-block; width: 200px; height: 200px;">
<img src="images/2.jpg" alt="" id="img">
</a>
</body>
<script>
document.getElementById("img").onclick=function () {
this.src=document.getElementById("a").href;
return false;
};
</script>
</html>
三、点击按钮切换图片
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>点击按钮切换图片</title>
</head>
<body>
<input type="button" value="显示大图" id="btn">
<img src="images/1.jpg" alt="" id="img">
</body>
<script>
function my$(id){
return document.getElementById(id);
}
my$("btn").onclick=function () {
my$("img").src="images/2.jpg";
}
</script>
</html>
四、设置idv样式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>设置idv样式</title>
</head>
<body>
<div id="hh"></div>
</body>
<script>
//凡是在css中的代码是多个单词的,在js中把-去掉,后面的单词的首字母大写,例如:background-color(css) -------> backgroundColor (js)
var div=document.getElementById("hh");
div.style.width="200px";
div.style.height="200px";
div.style.backgroundColor="pink";
</script>
</html>
五、点击按钮设置div显示或者隐藏
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>点击按钮设置div显示或者隐藏</title>
</head>
<style>
.cls{
width: 200px;
height: 200px;
background-color: pink;
/*display: none; !*隐藏div*!*/
}
</style>
<body>
<!--第一种方法-->
<!-- <div id="dd" class="cls">
</div>
<input type="button" id="btn1" value="显示">
<input type="button" id="btn2" value="隐藏">-->
<!-- <script>
//显示div
var btn1=document.getElementById("btn1");
btn1.onclick=function(){
document.getElementById("dd").style.display="block"; //显示div
}
//隐藏
var btn2=document.getElementById("btn2");
btn2.onclick=function(){
document.getElementById("dd").style.display="none"; //显示div
}
</script>-->
<!--第二种方法-->
<!--<div id="ddd" class="cls"></div>
<input type="button" value="隐藏" id="btn3">
<script>
document.getElementById("btn3").onclick=function () {
//判断当前点击的按钮value属性值
if (this.value=="隐藏") {
document.getElementById("ddd").style.display="none";
this.value="显示";
}
else if(this.value=="显示"){
document.getElementById("ddd").style.display="block";
this.value="隐藏";
}
}
</script>-->
<!--第三种方法-->
<input type="button" id="btn" value="设置样式">
<div id="dv"></div>
<script>
//设置div的样式 样式少的时候推荐这个方法添加
// document.getElementById("btn").οnclick=function () {
// var div=document.getElementById("dv");
// div.style.width="200px";
// div.style.height="200px";
// div.style.backgroundColor="pink";
// div.style.border="2px solid red";
// }
//添加calss类样式 样式多的时候推荐这个方法添加
//在js代码中,设置元素的样式,不要class这个属性,用className
document.getElementById("dv").className="cls";
</script>
</body>
</html
六、网页开关灯
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>网页开关灯</title>
<style>
.cls{
background-color: black;
}
</style>
</head>
<body id="bd">
<input type="button" id="btn" value="开、关灯">
<script>
document.getElementById("btn").onclick=function () {
document.body.className=document.body.className!="cls"?"cls":"";
}
</script>
</body>
</html>
七、 禁用文本
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>禁用文本</title>
</head>
<body>
<input type="button" value="禁用文本框" id="btn"/>
<input type="text" value="文本框" id="txt"/>
<script>
//先根据id获取按钮,为按钮注册点击事件,添加事件处理函数
document.getElementById("btn").onclick=function () {
//根据id获取文本框,设置disabled属性
document.getElementById("txt").disabled=true;
};
</script>
</body>
</html>
八、阻止超链接跳转
<!DOCTYPE html>
<html lang="en">
<head>
<meta
<title>阻止超链接跳转</title>
</head>
<body>
<script>
//点击超链接弹出对话框
//阻止超链接的默认的跳转:return false
</script>
<!--第一种写法:-->
<a href="http://www.baidu.com" onclick="alert('哎呀,我被点了'); return false">百度</a>
<br/>
<!--第二种写法-->
<script>
function f1() {
alert("哇塞,好漂亮哦");
return false;
}
</script>
<a href="http://www.baidu.com" onclick="return f1()">百度</a>
<!--第三种写法:-->
<a href="http://www.baidu.com" id="ak">百度</a>
<script>
document.getElementById("ak").onclick=function () {
alert("嘎嘎");
return false;
};
</script>
</body>
</html>
九、美女相册
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
body {
font-family: "Helvetica", "Arial", serif;
color: #333;
background-color: #ccc;
margin: 1em 10%;
}
h1 {
color: #333;
background-color: transparent;
}
a {
color: #c60;
background-color: transparent;
font-weight: bold;
text-decoration: none;
}
ul {
padding: 0;
}
li {
float: left;
padding: 1em;
list-style: none;
}
#imagegallery {
list-style: none;
}
#imagegallery li {
margin: 0px 20px 20px 0px;
padding: 0px;
display: inline;
}
#imagegallery li a img {
border: 0;
}
</style>
</head>
<body>
<h2>
美女画廊
</h2>
<ul id="imagegallery">
<li><a href="images/1.jpg" title="美女A">
<img src="images/1-small.jpg" width="100" alt="美女1"/>
</a></li>
<li><a href="images/2.jpg" title="美女B">
<img src="images/2-small.jpg" width="100" alt="美女2"/>
</a></li>
<li><a href="images/3.jpg" title="美女C">
<img src="images/3-small.jpg" width="100" alt="美女3"/>
</a></li>
<li><a href="images/4.jpg" title="美女D">
<img src="images/4-small.jpg" width="100" alt="美女4"/>
</a></li>
</ul>
<div style="clear:both"></div>
<!--显示大图的-->
<img id="image" src="images/placeholder.png" alt="" width="450"/>
<p id="des">选择一个图片</p>
<script src="common.js"></script>
<script>
//点击a标签,把a标签中的href的属性值给id为image的src属性
//把a的title属性的值给id为des的p标签赋值
//从ul中标签获取获取所有的a标签
var aObjs=my$("imagegallery").getElementsByTagName("a");
//循环遍历所有的a标签
for(var i=0;i<aObjs.length;i++){
//为每个a标签注册点击事件
aObjs[i].onclick=function () {
//为id为image的标签的src赋值
my$("image").src=this.href;
//为p标签赋值
my$("des").innerText=this.title;
//阻止超链接默认的跳转
return false;
};
}
</script>
</body>
</html>
十、隔行换色
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>隔行换色</title>
</head>
<body>
<input type="button" value="隔行换色" id="btn">
<ul id="ul">
<li>哈哈哈哈哈哈哈哈哈哈</li>
<li>嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎</li>
<li>哈哈哈哈哈哈哈哈哈哈</li>
<li>嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎</li>
<li>哈哈哈哈哈哈哈哈哈哈</li>
<li>嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎</li>
</ul>
<script>
document.getElementById("btn").onclick=function () {
var list=document.getElementById("ul").getElementsByTagName("li");
for (var i=0;i<list.length;i++){
/* if(i%2==0){
//偶数
list[i].style.backgroundColor="red";
}
else {
//奇数
list[i].style.backgroundColor="yellow";
}*/
list[i].style.backgroundColor=i%2==0?"red":"yellow";
}
}
</script>
</body>
</html>
十一、列表高亮显示
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>列表高亮显示</title>
<style>
ul{
list-style: none;
}
</style>
</head>
<body>
<ul>
<li>hujchsjhfcjodsvojwsnvbfrsbh</li>
<li>hujchsjhfcjodsvojwsnvbfrsbh</li>
<li>hujchsjhfcjodsvojwsnvbfrsbh</li>
<li>hujchsjhfcjodsvojwsnvbfrsbh</li>
<li>hujchsjhfcjodsvojwsnvbfrsbh</li>
<li>hujchsjhfcjodsvojwsnvbfrsbh</li>
</ul>
<script>
//鼠标进去和鼠标离开两个事件
//获取所有的li标签
var list=document.getElementsByTagName("li");
for (var i=0;i<list.length;i++){
//为li注册鼠标进去的事件
list[i].onmousemove=function () {
this.style.backgroundColor="yellow";
};
//为li注册鼠标离开的事件
list[i].onmouseout=function () {
this.style.backgroundColor="";
};
}
</script>
</body>
</html>
十二、显示和隐藏二维码
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
.nodeSmall {
width: 50px;
height: 50px;
background: url(images/bgs.png) no-repeat -159px -51px;
position: fixed;
right: 10px;
top: 40%;
}
.erweima {
position: absolute;
top: 0;
left: -150px;
}
.nodeSmall a {
display: block;
width: 50px;
height: 50px;
}
.hide {
display: none;
}
.show {
display: block;
}
</style>
</head>
<body>
<div class="nodeSmall" id="node_small">
<a href="#"></a><!--锚定-->
<div class="erweima hide" id="er">
<img src="images/456.png" alt=""/>
</div>
</div>
<script src="common.js"></script>
<script>
//获取鼠标要进入的a标签
//先获取最外面的div
var aObj=my$("node_small").getElementsByTagName("a")[0];
//为a注册鼠标进入
aObj.onmouseover=function () {
my$("er").className="erweima show";
};
//为a注册鼠标离开
aObj.onmouseout=function () {
my$("er").className="erweima hide";
};
</script>
</body>
</html>