经常会有类似开关效果的按钮,显示隐藏
<script>
var flag = true;
var getbox = document.getElementById("box");
var trigger = document.getElementById("trigger");
trigger.onclick = function(){
if(trigger.value == "隐藏"){
getbox.style.display = "none"
trigger.value = "显示";
}else{
getbox.style.display = "block";
trigger.value = "隐藏";
}
/*if(flag){
getbox.style.display = "none";
flag = false;
}else{
getbox.style.display = "block";
flag = true;
}*/
}
</script>
一种是通过定义一个布尔值来判断,另一种是通过变换按钮的value来判断,原理相似