常用方法:
常用的对象和方法:
1、String
2、Date
3、Math
4、Global
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>js常用方法和对象</title>
<script type="text/javascript">
function testString(){
var str=new String ("abcdefg");
var str2="MNP";
alert(str.toUpperCase());
alert (str2.toLowerCase());
var s="哈哈,呵呵,嘿嘿";
var s1=s.split(",");
alert(s1.length);
alert(s1);
alert(typeof s);
alert(typeof s1);
var s2="abcdef";
alert(s2.substr(1,3));
alert(s2.substring(1,3));
var s3="abcdefg";
alert(s.indexOf("dd"));
alert(s3.indexOf("f"));
}
function testDate(){
var d=new Date();
alert(d);
alert(d.getYear());
alert(d.getFullYear());
alert(d.getMonth()+1);
alert(d.getDate());
alert(d.getDay());
alert(d.getHours());
alert(d.getMinutes());
alert(d.getSeconds());
alert (d.getHours()+":"+d.getMinutes()+":"+d.getSeconds());
}
function testMath(){
alert("Math.random();"+Math.random());
alert(1000+Math.random()*9000);
alert (Math.floor("12.34"));
alert(Math.floor(1000+Math.random()*9000));
alert(Math.ceil("12.34"));
alert(Math.round(12.12));
alert(Math.round(12.65));
alert(Math.min(12,13,5,78));
alert(Math.max(12,3,4,56));
}
function TestGlobal(){
var str="var a=123";
eval(str);
alert(a);
alert(isNaN("123"));
alert(parseFloat("12.34a34a"));
alert(parseFloat("12a34.5a"));
alert(parseInt("a12a5s3"));
alert(parseInt("12.34a5s3"));
}
</script>
</head>
<body>
<input type="button" value="String学习" onclick="testString()" />
<input type="button" value="Date学习" onclick="testDate()" />
<input type="button" value="Math学习" onclick="testMath()" />
<input type="button" value="Global学习" onclick="TestGlobal()" />
</body>
</html>