1、JS提供的转换函数
2、toFixed()函数——把 Number 四舍五入为指定小数位数的数字
3、DOM动态给按钮绑定事件
4、设置Element的对象属性、集合属性和事件属性
5、sort()函数——Array.sort()方法是用来对数组项进行排序
6、Math对象中的random()、ceil()、floor()、round()等函数
7、document获取自定义属性
1、JS提供的转换函数
parseInt(value)——把给定的值转换成整数;
parseFloat(value)——把给定的值转换成浮点数;
Boolean(value)——把给定的值转换成Boolean型;
Number(value)——把给定的值转换成数字(可以是整数或浮点数);
String(value)——把给定的值转换成字符串;
2、toFixed()函数——把 Number 四舍五入为指定小数位数的数字
例如,5.05---->toFixed(1) 5.1
5.056-------->toFixed(2) 5.06
但是用到0.056时就出现问题了toFixed(1)的结果是0.0
有点奇怪的答案
下面的脚本是重写了toFixed(),这样0.056就可以转化到0.1了
Number.prototype.toFixed = function(len) {
var add = 0;
var s, temp;
var s1 = this + "";
var start = s1.indexOf(".");
if (s1.substr(start + len + 1, 1) >= 5) add = 1;
var temp = Math.pow(10, len);
s = Math.floor(this * temp) + add;
return s / temp;
}
3、DOM动态给按钮绑定事件
document.getElementById(testbt).onclick= function () { alert(This is a test!); }
4、设置Element的对象属性、集合属性和事件属性
document.getElementById(testbt).className = bordercss;
document.getElementById(testbt).style.cssText = color: #00f;;
document.getElementById(testbt).style.color = #00f;
document.getElementById(testbt).onclick= function () { alert(This is a test!); }
5、sort()函数——Array.sort()方法是用来对数组项进行排序
javascript 中 Array.sort(sortfunction)方法是用来对数组项进行排序的 ,默认情况下是进行升序排列
sort() 方法可以接受一个 方法为参数 ,这个方法有两个参数。分别代表每次排序比较时的两个数组项。sort()排序时每次比较两个数组项都回执行这个参数,并把两个比较的数组项作为参数传递给这个函数。当函数返回值为1的时候就交换两个数组项的顺序,否则就不交换。
var arrA = [6, 2, 4, 3, 5, 1];
//升序
function asc(x, y) {
if (x > y)
return 1;
if (x < y)
return -1;
}
//降序
function desc(x, y) {
if (x > y)
return -1;
if (x < y)
return 1;
}
//随机
function random() {
return Math.random() > 0.5 ? -1 : 1;
}
//升序排序
arrA.sort(asc);
document.writeln(arrA);
document.writeln("<br>");
//降序排序
arrA.sort(desc);
document.writeln(arrA);
document.writeln("<br>");
//随机排序
arrA.sort(random);
document.writeln(arrA);
将上述代码加以封装改进:
var arrA = [6, 2, 4, 3, 5, 1];
//数组升序排序
Array.prototype.AscSort = function() {
return this.sort(function(x, y) {
if (x > y)
return 1;
if (x < y)
return -1;
});
};
//数组降序排序
Array.prototype.DescSort = function() {
return this.sort(function(x, y) {
if (x > y)
return -1;
if (x < y)
return 1;
});
};
//数组随机排序
Array.prototype.RandomSort = function() {
return this.sort(function() {
return Math.random() > 0.5 ? -1 : 1;
});
};
//升序排序
arrA.AscSort();
document.writeln(arrA);
document.writeln("<br>");
//降序排序
arrA.DescSort();
document.writeln(arrA);
document.writeln("<br>");
//随机排序
arrA.RandomSort();
document.writeln(arrA);
6、Math对象中的random()、ceil()、floor()、round()等函数
Math.random():返回0-1之间的随机数
从任意值开始 至 任意值
parseInt(Math.random()*(上限-下限+1)+下限);
//随机整数(指定范围内的随机整数)
function fRandomBy(under, over) {
switch (arguments.length) {
case 1: return parseInt(Math.random() * under + 1);
case 2: return parseInt(Math.random() * (over - under + 1) + under);
default: return 0;
}
}
document.write(fRandomBy(1, 100)); //输出指定范围内的随机数的随机整数
Math.ceil():可对一个数进行上舍入(向上取整,有小数就整数部分加1)
Math.floor():可对一个数进行下舍入(向下取整)
Math.round():可把一个数字舍入为最接近的整数
更多:
Math.abs(x): 返回数的绝对值
Math.acos(x): 返回数的反余弦值
Math.asin(x): 返回数的反正弦值
Math.atan(x): 以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值
Math.atan2(y,x): 返回从 x 轴到点 (x,y) 的角度(介于 -PI/2 与 PI/2 弧度之间)
Math.ceil(x): 对一个数进行上舍入
Math.cos(x):返回数的余弦
Math.exp(x): 返回 e 的指数。
Math.floor(x): 对一个数进行下舍入。
Math.log(x):返回数的自然对数(底为e)
Math.max(x,y):返回 x 和 y 中的最高值
Math.min(x,y):返回 x 和 y 中的最低值
Math.pow(x,y):返回 x 的 y 次幂
Math.random():返回 0 ~ 1 之间的随机数
Math.round(x):把一个数四舍五入为最接近的整数
Math.sin(x):返回数的正弦
Math.sqrt(x):返回数的平方根
Math.tan(x):返回一个角的正切
Math.toSource():代表对象的源代码
Math.valueOf():返回一个 Math 对象的原始值
7、document获取自定义属性
<input type="text" id="txtMsg" myAttr="abc" />
this.onload = function() {
var val = document.getElementById("txtMsg").attributes["myAttr"].value;
alert(val);
}

本文介绍了JavaScript中的关键函数,如转换函数、toFixed()、DOM事件绑定等,还讲解了Array.sort()的不同用法及Math对象的多种实用函数,并演示了如何获取自定义属性。
2万+

被折叠的 条评论
为什么被折叠?



