条件判断4个代码片段

来源:互联网 发布:软件测试中的单元测试 编辑:程序博客网 时间:2024/06/18 15:14
  1. if(…)else(…)
  2. if(…)else if(…)else if(…)else if(…)else(…)
  3. if判断里面的“=” 用“==”;(注意啊~ ~)

代码1

  1. 代码一`



    Document

    window.onload=function(){ var oBtn1=document.getElementById("btn1"); var oBtn2=document.getElementById("btn2"); var oP=document.getElementById("p1");
        var num=15;    oBtn2.onclick=function(){        //alert("1");  JS中不允许出现"-".        if (num<25) {                num++;                oP.style.fontSize=num +"px";                    }        alert(num);    }    oBtn1.onclick=function(){        //alert("1");  JS中不允许出现"-".        if (num>12) {                num--;                oP.style.fontSize=num +"px";                    }        alert(num);    }}





    赵丽颖在不是很红的时候朋友并不多,小明星嘛,不受重视,助理也没有,多的是深夜自己陪自己一个人独处。现在火了,粉丝有了,团队也跟上了,她再也不是娱乐圈的小透明,照顾她关心她的人都可以绕地球一整圈。

可即使如此,赵丽颖依旧没有什么明星架子,跟那些出名的不出名的都能很好的成为朋友,因为她一直是一个懂得感恩的人,哪怕别人不经意之间对她有所关心,赵丽颖都很感激的记在心里,就连张睿只是顺手给她做了一顿年夜饭都能让其在节目里“失控”落泪!

`

代码2

<!doctype html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title>    <style>        #div1{width: 240px;height: 200px;border:1px solid #333;background-color: #f1f1f1; padding:10px;}    </style>    <script>        window.onload=function(){            var oDiv=document.getElementById("div1");            var oSpan=document.getElementById("span1");            var oText=document.getElementById("text1");            var oBtn=document.getElementById("btn1");            oBtn.onclick=function(){                if (oText.value=="") {                    alert("好歹输入带你东西啊~ ~")                }else{                    oDiv.innerHTML+="妙味:"+oText.value+"<br>";                    oText.value="";                }            }        }    </script></head><body>    <div id="div1"></div>    <span id="span1">妙味:</span>    <input id="text1" type="text">    <input id="btn1" type="button"value="提交"></body></html>

代码3

<!doctype html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title>    <script>        window.onload=function(){            var oText=document.getElementById("text1");            var oBtn=document.getElementById("btn1");            oBtn.onclick=function(){                if(oText.value==""){                    alert("恭喜你写点吧   ^^")                }                if(oText.value=="CSS"){                    alert("恭喜你通过验证^^")                    oBtn.style.background="green";                }                else if(oText.value=="JS"){                    alert("恭喜你通过验证^^")                    oBtn.style.background="green";                }                else if(oText.value=="HTML"){                    alert("恭喜你通过验证^^")                    oBtn.style.background="green";                }                else if(oText.value=="vue"){                    alert("恭喜你通过验证^^")                    oBtn.style.background="green";                }                else if(oText.value=="angular"){                    alert("恭喜你通过验证^^")                    oBtn.style.background="green";                }                else{                    alert("我是外星人,你的验证不通过哦~ ~")                }            }        }    </script></head><body>    通关口令:<input id="text1" type="text">    <input id="btn1" type="button"value="验证"></body></html>

代码4

<!doctype html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title>    <script>        window.onload=function(){            var oImg=document.getElementById("image1");            var onOff=true;//创建一个开关,布尔值,true真1,false假0.            oImg.onclick=function(){                if(onOff){                    oImg.src="img/img2.jpg";                    onOff=false;                }else{                    oImg.src="img/img1.jpg";                    onOff=true;                 }            }        }    </script></head><body>    <img id="image1" src="img/img1.jpg" alt=""></body></html>
原创粉丝点击