计算机:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>计算机</title>
<script type="text/javascript">
window.onload = function(){
var input01 = document.getElementById("input01");
var select = document.getElementById("select");
var input02 = document.getElementById("input02");
var bth = document.getElementById("btn");
btn.onclick = function(){
input01.value;
input02.value;
switch (select.value){
case '0'://+
alert((parseFloat(input01.value)100 + parseFloat(input02.value)100)/100);
break;
case '1'://-
alert((parseFloat(input01.value)100) - (parseFloat(input02.value)100)/100);
break;
case '2'://*
alert((parseFloat(input01.value)100) * (parseFloat(input02.value)100)/10000);
break;
case '3':// /
alert((parseFloat(input01.value)100) / (parseFloat(input02.value)100));
break;
}
}
}
</script>
</head>
<body>
<h1>计算机</h1>
<input type="text" name="" id="input01" />
<select id="select">
<option value="0">+</option>
<option value="1">-</option>
<option value="2">*</option>
<option value="3">/</option>
</select>
<input type="text" name="" id="input02" />
<input type="button" name="" value="计算" id="btn" />
</body>
</html>
判断今天星期几:
var str = prompt('请输入数字1-7');
if (str > '7' || str < '1') {
alert('输入不合法');
return;
}
switch (str){
case '7':
alert('星期日');
break;
case '6':
alert('星期六');
break;
case '5':
alert('星期五');
break;
case '4':
alert('星期四');
break;
case '3':
alert('星期三');
break;
case '2':
alert('星期二');
break;
case '1':
alert('星期一');
break;
}
};
份数是否及格:
window.onload = function () {
var score = prompt('请输入分数');
switch (parseInt(score/60)) {
case 1:
alert('合格');
break;
case 0:
alert('不合格');
break;
}
}
通过标签获取元素
document.getElementsByTagName();标签获取元素
循环
有规律重复可以用循环来做
for循环
for(var i=0; i<8; i++){
aLi[i].style.background='gold'
}
window.onload = function () {
var olist = document.getElementById('list01');
var ali = olist.getElementsByTagName('li');
for(var i=0;i<8;i++){
if(i%2 == 0){
ali[i].style.backgroundColor = 'gold';
}
}
i++;
}
while循环
var i=0;
while (i<aLi.length){
if(i%2==0){
aLi[i].style. background='gold';
}
i++;
}
do...while循环
var i = 0;
do {
if (i % 2 == 0) {
ali[i].style.backgroundColor = 'gold';
}
i++;
} while (i < ali.length);
}
while和do while的区别:
do while是大括号里无论如何都先执行一次,执行完再判断是否成立
while是先判断是否成立,然后在执行大括号的内容
数组去重
var aRr=[1,3,4,1,6,9,1,2,5,3,1,6,5,4,4];
var aRr2=[];
for(var i=0;i<aRr.length;i++){
if (aRr.indexof(aRr[1])==1{
aRr2.push(aRr[i]);
}
}
alert(aRr2);
字符串处理
var str="2018-12-03";
var arr=str.split('-');//2018,12,03
alert(arr);
var arr = str.split('')//"2","0","1","8","-","1","2","-","0","3"
console.log(arr);
touppercase()转大写
tolowercase()转小写
字符串反转
var str = "12sdadsad5646sadsadas";
//spilt()字符串转为数组
//reverse()数组反转
//join()数组转成字符串
var str1 = str.split('').reverse().join('')
alert(str1)
先用spilt方法将字符串转成数组,然后在用数组反转,最后用join将数组转成字符串