Js的学习第五天

依旧是课堂的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<a>一个数:</a><input type="text" id="num1" /><br>
<a>另一个数:</a><input type="text" id="num2" /><br>
<input type="button" value="相加" onclick="oldofnew('+')">
<input type="button" value="相减" onclick="oldofnew('-')">
<input type="button" value="相乘" onclick="oldofnew('*')">
<input type="button" value="相除" onclick="oldofnew('/')">
<br>
<a>结果:</a><input type="text" id="newnum" />
<script>
function oldofnew(x){
// 这个地方做输入判断,对输入的value进行里面是否有非法字符
// var re = /^-.?[0-9]+.?[0-9]*$/;
// 如果是判断正整数
// var re = /[0-9]+[0-9]*]*/;
// 上面注释掉的方法不好,别使用
var num11= document.getElementById("num1").value
var num22= document.getElementById("num2").value
// 这个方法可以正确区分数字和字符,同时,不会对科学技术法产生影响
if (isNaN(num11) || isNaN(num22))
alert("非法字符");
else switch(x){
case '+':var sumnum = Number(document.getElementById("num1").value) +Number(document.getElementById("num2").value);
document.getElementById("newnum").value=sumnum;break;
case '-':var sumnum = Number(document.getElementById("num1").value) -Number(document.getElementById("num2").value);
document.getElementById("newnum").value=sumnum;break;
case '*':var sumnum = Number(document.getElementById("num1").value) *Number(document.getElementById("num2").value);
document.getElementById("newnum").value=sumnum;break;
// case '/':var sumnum = Number(document.getElementById("num1").value) /Number(document.getElementById("num2").value);
// document.getElementById("newnum").value=sumnum;break;
case '/':
if(Number(document.getElementById("num2").value)==0){
alert("被除数不能是0");
}
else{
var sumnum = Number(document.getElementById("num1").value) /Number(document.getElementById("num2").value);
document.getElementById("newnum").value=sumnum;break;
}
}
}
</script>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>大小写转换</h1><br>
<a>原数据:</a><input type="text" id="oldword"/><br>
<a>操作:</a><input type="button" onclick="oldofnew(1)" value="转大写" />
<input type="button" onclick="oldofnew(2)" value="转小写"><br>
<a>新数据:</a><input type="text" id="newword"/>
<script>
// document.getElementById("oldword").value;
function oldofnew(s){
switch(s){
case 1: var new1 = document.getElementById("oldword").value.toUpperCase();document.getElementById("newword").value=new1;break;
case 2: var new2 = document.getElementById("oldword").value.toLowerCase();document.getElementById("newword").value=new2;
}
}
// function oldofnew1(){
// var new1 = document.getElementById("oldword").value.toUpperCase();
// document.getElementById("newword").value=new1;
// }
// function oldofnew2(){
// var new2 = document.getElementById("oldword").value.toLowerCase();
// document.getElementById("newword").value=new2;
// }
</script>
</body>
</html>