JS Date对象 Math对象 全局函数 bom对象 函数重载
JS中的Date对象
获取当前时间
var date = new Date();
document.write( date );//获取到当前的时间日期,但不是中文的,不符合我国的风格
将时间格式修改为本地的时间格式
toLocaleString()
var date = new Date();
document.write( date );//获取到当前的时间日期,但不是中文的,不符合我国的风格
document.write("<br/");
document.write( date.toLocaleString() );//转换成我们习惯的格式
获取当前年份
getFullYear():得到当前年份(4位)
var date = new Date();
document.write( date.getFullYear() );
获取当前月份
getMonth():获取当前的月份,是0~11 不是 “1~12”,所以要手动加1
var date = new Date();
document.write( date.getMonth() + 1 );
获取当前是星期几
getDay():获取今天是星期几,但是0-6而不是1-7,星期天是“0”
var date = new Date();
document.write( date.getDay() );
获取当前是几号
getDate():获取当前是几号,返回的是1-31
var date = new Date();
document.write( date.getDate() );
获取当前的时间
getHours():获取小时
getMinutes():获取分钟
getSeconds():获取秒
getMilliseconds():获取毫秒
var date = new Date();
document.write( date.getHours() );
document.write("<br/");
document.write( date.getMinutes() );
document.write("<br/");
document.write( date.getSeconds() );
document.write("<br/");
document.write( date.getMilliseconds() );
获取毫秒数(从1970年1月1日开始至今)
getTime():获取从19701月1日至今的毫秒数,一般应用在处理缓存中
JS中的Math对象
数学的运算,里面的方法都是静态方法,可以直接使用不需要new 。
小数的舍取
ceil(x):向上舍
floor(x): 向下舍
round(x):四舍五入
随机数
random():随机范围0-1之间的伪随机数
案例:获取0-9的一个随机数:
doucument.write( Math.floor( Math.random()*10));
最大值,最小值
max(x,y):返回x,y中最大的数
min(x,y):返回x,y中最小的数
幂
pow(x,y):返回x的y次幂
圆周率(属性)
PI:返回圆周率(约等于3.14159)
JS的全局函数
什么是全局函数
- 不属于任何对象,直接写名称使用
- 例如eval()
eval()
如果字符串是一段代码,那么用这个方法,可以直接将字符串当做JS代码执行。
var str = "alert(‘123456’)";
eval(str);
编码与解码
encodeURI():对字符进行编码
decodeURI(): 对字符进行解码
var str = "中文字符abc123";
doucument.write(str);
var strEn = encodeURI(str);
doucument.write(strEn);
var strDe = decodeURI(strEn);
doucument.write(steDe)
判断是否为数字
isNaN(): 判断字符串是否是数字,不是返回true,是返回false.
类型转换
parseInt()
var str = "123"
var str1 = "123"
document.write(str + 1);
document.write("<br/>");
document.write(parseInt(str) + 1);
函数重载
- JS中不存在函数重载
- 可以通过其他方式来模拟重载
以下代码说明,没有函数重载,前面两次调用得到的结果为NaN,只有最后一次得到正确结果
function add(a,b){
sum = a + b;
return sum;
}
function add(a,b,c){
sum = a + b + c;
return sum;
}
function add(a,b,c,d){
sum = a + b + c + d;
return sum;
}
alert(add(1,2)); //NaN
alert(add(1,2,3));//NaN
alert(add(1,2,3,4));//10
在每个函数中都有一个数组arguments,函数的参数会传入到这个数组中。
以下代码说明
function add(a,b){
alert(arguments.length)
sum = a + b;
return sum;
}
alert(add(1,2));
alert(add(1,2,3));
alert(add(1,2,3,4));
那么我们就可以利用这个数组来模拟重载
function add(){
if(arguments.length == 2){
return arguments[0] + arguments[1];
}else if(arguments.length == 3){
return arguments[0] + arguments[1] + arguments[2];
}else if(arguments.length == 4){
return arguments[0] + arguments[1] + arguments[2] + arguments[3];
}else{
return -1;
}
}
alert(add(1,2));
alert(add(1,2,3));
alert(add(1,2,3,4));
JS 的bom对象
bom:浏览器对象
navigator 获取客户机(浏览器)的信息
navigator.appName:获取浏览器名称
document.writ(navigator.appName);
screen 获取屏幕的信息
width: 返回显示屏幕的高度。
height:返回显示器屏幕的宽度。
document.writ(screen.width);
document.writ(screen.height);
location 获取请求地址
href:设置或返回完整的 URL。
以下示例会在当前页面显示页面路径,如果点击按钮后,会跳转到百度页面
html代码
<!-- onclick 与go函数绑定,一旦点击这个按钮则会调用go()函数 -->
<input type="button" value="跳转" onclick="go();">
JS代码
document.writ(location.href);//获取当前URL
//设置URL
function go(){
location.href = "http://www.baidu.com"
}
history 获取请求的url历史记录
history.back() : 加载 history 列表中的前一个 URL。
history.forward() : 加载 history 列表中的下一个 URL。
window对象
窗口对象
顶层对象,上面介绍的对象全都在window中
一般可以简写省略,例如history.back()
实际上是这样的window.history.back()
常用方法
window.confirm():显示带有一段消息以及确认按钮和取消按钮的对话框。有一个返回值,如果点击确定,返回true,点击 取消,则返回false
prompt(“title”,“text”): 显示可提示用户输入的对话框。第一个参数是输入框的提示标题,第二个是默认的文本值
open( 打开新窗口的地址url,"",窗口特征比如宽高) :打开一个新的浏览器窗口或查找一个已命名的窗口。
close():关闭当前窗口
定时器
-
setInterval(code,millisec[,“lang”]) 按照指定的周期(以毫秒计)来调用函数或计算表达式。
code:代码,必填
millisec:毫秒数(1秒等于1000毫秒),即多久执行一次code. -
setTimeout(code,millisec) :在指定的毫秒数后调用函数或计算表达式,但只执行1次,之后就不会再执行了。
code:代码,必填
millisec:毫秒数(1秒等于1000毫秒),即多久之后执行code.
clearInterval(id_of_setinterval) :取消由 setInterval() 设置的 timeout。
clearTimeout(id_of_setinterval): 取消由 setTimeout() 方法设置的 timeout。
清除的使用演示
var gg = setInterval("aler("GG",3000)");
var qq= setTimeout("aler("QQ",3000)");
function clearSetInterval(){
clearInterval(gg)
}
function clearSetTimeout(){
clearTimeout(qq)
}