<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>javascript函数定义</title></head><body><script>// 函数表达式varmyFunction=function(a, b){return a * b
};var z =myFunction(4,3);
console.log(z);// 箭头函数// 之前:// var x = function(x, y) {// return x * y;// }constx=(x, y)=> x * y;// js函数参数 : 函数显示参数(Parameters)与隐式参数(Arguments)</script></body></html>
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>JS的number对象</title></head><body><!-- 使用toString()方法输出16进制等 --><script>var myNumber =128;
myNumber.toString(16);
myNumber.toString(8);
myNumber.toString(2);</script>;
<!-- +-Infinity --><!-- 除以0产生了无限 --><script>var x =2/0;var y =-2/0;</script><!-- 在字符串中查找字符串 --><pid="p1">Click the button to locate where "locate" first occurs.</p><pid="p2">0</p><buttononclick="myFunction()">点我</button><script>functionmyFunction(){var str = document.getElementById("p1").innerHTML;var n = str.indexOf("locate");
document.getElementById("p2").innerHTML = n +1;}</script><!-- 内容匹配 --><!-- match()函数用来查找字符串中特定的字符,并且如果找到的话,则返回这个字符。 --><hr><script>var str ="Hello world!";
document.write(str.match("world")+"<br>");
document.write(str.match("World")+"<br>");
document.write(str.match("world!"));</script><!-- replce替换内容 --><script>
str ="Please visit Microsoft!"var n = str.replace("Microsoft","Runoob");
console.log(n);</script><!-- 字符串转为数组 : 使用split()函数 --><script>
txt ="a,b,c,d,e";
txt.split("9");// 使用,分隔
console.log(txt);
txt.split("|");// 使用,分隔
console.log(txt);
txt.split("q");// 使用,分隔
console.log(txt);
txt.split("s");// 使用,分隔
console.log(txt);</script></body></html>
3-4 date.html
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>Date对象</title></head><bodyonload="startTime()">
这里显示的是页面刷新之后的newDate :::::
<iid="newDate"></i><pid="demo">点击按钮展示相应按钮的变化</p><buttononclick="myFunction()">点我获取当前年份: getFullYear()</button><br><hr><buttononclick="myFunction2()">点我获取1970年1月1日至今的毫秒数: getTime()</button><br><hr><i>记住JS月份是从0到11,10是11月</i><buttononclick="myFunction3()">使用setFullYear()设置具体的日期,出现的是毫秒数,离谱事件</button><br><hr><buttononclick="myFunction4()">点击将utc日期和时间转换字符串</button><hr><hr><buttononclick="myFunction5()">使用getDay()来显示星期,而不仅仅是数字</button><hr><divid="txt"></div><!-- 使用Date()方法获得当前的日期 --><script>
document.getElementById("newDate").innerHTML =newDate();functionmyFunction(){var d =newDate();var x = document.getElementById("demo");// 获取年份
x.innerHTML = d.getFullYear();}functionmyFunction2(){var d =newDate();var x = document.getElementById("demo");// 获取获取1970年1月1日至今的毫秒数
x.innerHTML = d.getTime();}functionmyFunction3(){var d =newDate();var x = document.getElementById("demo");// 使用setFullYear()设置具体的日期
x.innerHTML = d.setFullYear(2021,11,5);
document.write(d);}functionmyFunction4(){var d =newDate();var x = document.getElementById("demo");// 使用setFullYear()设置具体的日期
x.innerHTML = d.toUTCString();}functionmyFunction5(){var d =newDate();var weekday =newArray(7);
weekday[0]="周日";
weekday[1]="周一";
weekday[2]="周二";
weekday[3]="周三";
weekday[4]="周四";
weekday[5]="周五";
weekday[6]="周六";var x = document.getElementById("demo");
x.innerHTML = weekday[d.getDay()];}functionstartTime(){var today =newDate();var h = today.getHours();var m = today.getMinutes();var s = today.getSeconds();// 在小于10的数字前面加一个'0'
m =checkTime(m);
s =checkTime(s);
document.getElementById('txt').innerHTML = h +":"+ m +":"+ s;
t =setTimeout(function(){startTime()},1000);}functioncheckTime(i){if(i <10){
i ="0"+ i;}return i;}</script></body></html>