1, display-week code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>now test</title>
</head>
<body>
<p>点击下面的按钮来显示今天是周几:</p>
<button onclick="myFunction()">点击这里</button>
<p id="demo"></p>
<script>
function myFunction(){
var x;
var d=new Date().getDay();
switch(d){
case 0:x="今天是星期天";
break;
case 1:x="今天是星期一";
break;
case 2:x="今天是星期2";
break;
case 3:x="今天是星期三";
break;
case 4:x="今天是星期四";
break;
case 5:x="今天是星期五";
break;
case 6:x="今天是星期六";
break;
}
document.getElementById("demo").innerHTML=x;
}
</script>
</body>
</html>
2, a warning is displayed
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>now test</title>
<script>
function myFunction(){
alert("hello \n我是一个警告框!");
}
</script>
</head>
<body>
<input type="button" onclick="myFunction()" value="显示警告"/>
</body>
</html>
3, a confirmation box
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>now test</title>
</head>
<body>
<p>点击按钮,显示确认框</p>
<button onclick="myFunction()">点我点我</button>
<p id="demo"></p>
<script>
function myFunction(){
var x;
var r=confirm("按下按钮");
if(r==true){
x="你按下了\"确定\"按钮!";
}
else{
x="你按下了\"取消\"按钮!";
}
document.getElementById("demo").innerHTML=x;
}
</script>
</body>
</html>
4, balloon
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>now test</title>
</head>
<body>
<p>点击按钮,显示确认框</p>
<button onclick="myFunction()">点我点我</button>
<p id="demo"></p>
<script>
function myFunction(){
var x;
var person=prompt("请输入你的名字","黄金");
if(person!=null&&person!=""){
x="hello"+person+"how are you?";
document.getElementById("demo").innerHTML=x;
}
}
</script>
</body>
</html>
5, calling a function with parameters
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>now test</title>
</head>
<body>
<p>点击按钮,来调用带参数的函数。</p>
<button onclick="myFunction('黄金','designer')">点我点我</button>
<script>
function myFunction(name,job){
alert("welcome"+name+",the"+job);
}
</script>
</body>
</html>
6, a function call with parameters 2
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>now test</title>
<script>
function myFunction(txt){
alert(txt);
}
</script>
</head>
<body>
<form>
<input type="button" onclick="myFunction('Good morning!')" value="早上">
<input type="button" onclick="myFunction('Good Evening!')" value="晚上">
</form>
<p>
当你点击其中任意一个按钮,一个函数将被执行,函数结果弹出一个警告显示传递的参数。
</p>
</body>
</html>
7, the return value of the function
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>now test</title>
<script>
function myFunction(){
return ("Hello word!");
}
</script>
</head>
<body>
<script>
document.write(myFunction())
</script>
</body>
</html>
8, with the function arguments and return values of
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>now test</title>
</head>
<body>
<p>本例调用的函数会执行一个计算,然后返回结果:</p>
<p id="demo"></p>
<script>
function myFunction(a,b){
return a*b;
}
document.getElementById("demo").innerHTML=myFunction(4,3);
</script>
</body>
</html>