1. js实现小数转百分比
Number.prototype.toPercent = function(n) {
n = n || 0;
return (Math.round(this*Math.pow(10, n + 2)) / Math.pow(10, n)).toFixed(n) + '%';
}
var A = 0.358975, B = 0.2554;
alert([A, A.toPercent(3), B, B.toPercent()].join('\n'));
2.how to restrict bootstrap date picker from future date
<script>
$(function() {
$('.datepicker').datepicker({
format: 'mm-dd-yyyy',
endDate: '+0d',
autoclose: true
});
});
</script>
3.打开新页面的两种方式
(1)超链接:<a href="http://wwww.herowalking.com">在当前窗口打开新页面</a>
等效于js代码:window.location.href = "http://www.herowalking.com";
(2)超链接:<a href="http://www.herowalking.com" target="_blank">在新窗口打开新页面</a>
等效于js代码:window.open("http://wwww.herowalking.com");
AngularJS中打开新页面的两种方式
(1)$window.location.href = "http://www.herowalking.com";
(2)$window.open("http://wwww.herowalking.com");
4. 判断是否为null, undefined
var exp = null;
if (!exp && typeof exp != "undefined" && exp != 0)
{
alert("is not null");
}
typeof exp != "undefined" 排除了 undefined;
exp != 0 排除了数字零和 false。
更简单的正确的方法:
var exp = null;
if (exp === null)
{
alert("is null");
}
尽管如此,我们在 DOM 应用中,一般只需要用 (!exp) 来判断就可以了,因为 DOM 应用中,可能返回 null,可能返回 undefined,如果具体判断 null 还是 undefined 会使程序过于复杂。
5.JS中的“&&”与“&”和“||”“|”有什么区别?
&&和||是逻辑运算的,返回的是boolean值,&和|是位运算的
他们最大的区别是,&& 和|| 是短路的,&和|不是短路的。
var a = 1;
var b = 1;
如if(a>2 && b<2) 和if(a>2 & b<2)
前者只会执行到a>2而b<2是不会执行的,返回false
后者执行a>2还会继续执行b<2,最终返回0