请问为什么我这样没法实现效果呢
来源:2-8 DOM0级事件
_麦当
2019-10-07 15:53:37
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>2-9</title>
<style>
.lock{width:140px;height:30px;line-height: 30px;background:#00f;
color:#fff;font-size:14px;text-align:center;border-radius:5px;
cursor:pointer;margin-top:30px;}
.unlock{width:140px;height:30px;line-height: 30px;background:#666;
color:#ccc;font-size:14px;text-align:center;border-radius:5px;
cursor:pointer;margin-top:30px;}
</style>
</head>
<body>
<div class="lock" id="btn">锁定</div>
<script type="text/javascript">
//获取按钮
var btn=document.getElementById("btn");
btn.onclick=function(){
//第一种方法
if(this.className=="lock"){
this.className=="unlock";
this.innerHTML=="解锁";
}else{
this.className=="lock";
this.innerHTML=="锁定";
}
//第二种方法
// if(this.innerHTML=="锁定"){
// this.className=="unlock";
// this.innerHTML=="解锁";
// }else{
// this.className=="lock";
// this.innerHTML=="锁定";
// }
}
</script>
</body>
</html>2回答
同学你好,
自己能够解决问题是很棒的哦!
一个等号是赋值,两个等号或者三个等号才是判断哦,所以在if条件中用两个等号进行判断class值是否为lock,下面赋值用一个等号。
自己再理解下,祝学习愉快!
_麦当
提问者
2019-10-07
好吧我搞懂了,是因为if和else里面的=多打了一个。
相似问题