您的位置:首页 > Web前端 > JavaScript

javascript学习笔记,绝对值,倒计时,按数字计算

2013-04-15 09:34 597 查看
1.javascript强制按照数字计算:

var a="12";
var b="34";
alert(a+b);// 输出"1234"
alert((+a)+(+b));// 输出"46"


2.javascript取绝对值:

alert(Math.abs(-1));// 输出1
alert(Math.abs(1));// 输出1


3.javascript倒计时:

var time=3;
var timeStr=["零","一","二","三"];
setInterval("send()",1000);
function send(){
if(time>0){
document.getElementById("num").innerHTML=timeStr[time-1];
time--;
return;
}
window.parent.location.replace("<%=basePath%>#");
}


4.js+css实现禁止选择文字:

1.firefox中给需要禁止选择文字的地方,加上样式:-moz-user-select:none;
2.ie中给需要禁止选择文字的标签,加上onselectstart="return false"事件


5.js向上向下取整,四舍五入:

1. Math.ceil()用作向上取整。
2. Math.floor()用作向下取整。
3. Math.round() 我们数学中常用到的四舍五入取整。
alert(Math.ceil(10/3));// 输出4
alert(Math.floor(10/3));// 输出3
alert(Math.round(10/3));// 输出3


6.js保留两位小数

12.12333.toFixed(2)-->12.12
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: