<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>js中常用对象和方法学习</title>
<script type="text/javascript">
// function testDate(){
// alert("测试date")
// }
</script>
<script src="my.js" type="text/javascript" charset="utf-8"></script>
</head>
<!--
js的常用对象和方法学习:
date日期对象:date对象需要new创建
注意:获取的是客户端的时间
获取年份
获取月
获取日
获取星期
获取小数
获取分钟
获取秒数
String字符对象,直接声明字符串使用即可
大小写转换toUpercase,tolowercase
字符串切割:split
字符串截取:sbustring,substr
Math对象:直接对象名.方法名使用,Math对象不能new创建
向上取整ceiL
向下取整:floor
随机数:random
四舍五入:round()
Global对象:直接方法即可
eval
inNaN
parseInt
parseFloat
-->
<body>
<h2>js中常用的对象和方法学习</h2>
<hr />
<input type="button" name="" id="" value="测试date" onclick="testDate()" />
<input type="button" name="" id="" value="测试string学习" onclick="testString()"/>
<input type="button" name="" id="" value="测试math学习" onclick="testMath()"/>
<input type="button" name="" id="" value="测试Global" onclick="testGlobal()"/>
</body>
</html>
function testonclick(){
alert("我是测试JS的单机事件");
}
function testondbclick(){
alert("我是测试JS的双击事件");
}
function testonmouseover(){
alert("我是测试鼠标移入事件");
}
function testonmouseout(){
alert("我是测试鼠标移出事件");
}
function testonmousemove(){
alert("我是测试鼠标移动事件");
}
function testonkeydown(){
alert("我是测试按下鼠标事件")
}
function testonkeyup(){
alert("我是测试鼠标抬起事件");
}
function testonfocus(){
alert("测试获取焦点");
}
function testonblur(){
alert("测试失去焦点");
}
function testonchange(){
alert("测试值改变事件");
}
function testDate(){
//获取date对象
var d=new Date();
//获取年月日
alert(d.getYear());//获取1900年至今的年份数据
alert(d.getFullYear());//获取当年的年分数
alert(d.getMonth()+1);//获取当前的月份数,但是需要+1
alert(d.getDay());//获取当前的星期书,注意星期日是0
alert(d.getDate());//获取当期那的日期
//获取时间
alert(d.getHours());//获取当前的小时数
alert(d.getMinutes());//获取当前的分钟数
alert(d.getSeconds());//获取当前的秒数
}
function testString(){
//创建字符串
var str="abc-de-fg";
//转换大小写
alert(str.toUpperCase());
//转换小写
alert(str.toLowerCase());
//首字母大写
alert(str.substring(0,1).toUpperCase()+str.substring(1).toLowerCase());
//split分割
var s=str.split("-");
for(var i=0;i<s.length;i++){
alert(s[i])
}
}
function testMath(){
//创建数字
var num=123.456;
alert(Math.ceil(num));//向上取整
alert(Math.floor(num));//向下取整
alert(Math.random());//取随机数
alert(Math.floor(Math.random()*9000+1000));//取出四位随机数
alert(Math.round(num));//返回最接近的整数,四舍五入
}
function testGlobal(){
//eval()方法,将字符串转换成为js代码执行
var str="var a=3";
eval(str);
alert(a);
var s="aaa";
if(isNaN(s)){
alert("不是数字");
}
//parseInt
alert(parseInt("1233asd123"));
alert(parseFloat("12.34566asdf"))
}