日期转化
var format = "yyyy-MM-dd hh:mm:ss";
dateFormat(newarr[x].ctime, format);
function dateFormat(longTime, formatStr) {
var d = new Date();
d.setTime(longTime);
return d.format(formatStr)
}
判断键盘大小写:
<form action="#" method="post">
<p><label for="password">密码:</label><input type="text" id="password" name="password" /><span class="showhide" style="display:none;">大写锁定键被按下,请注意大小写</span></p >
</form>
<script type="text/javascript">
function detectCapsLock(event){
var e = event||window.event;
var keyCode = e.keyCode||e.which; // 按键的keyCode
var isShift = e.shiftKey ||(keyCode == 16 ) || false ; // shift键是否按住
if (
((keyCode >= 65 && keyCode <= 90 ) && !isShift) // Caps Lock 打开,且没有按住shift键
|| ((keyCode >= 97 && keyCode <= 122 ) && isShift)// Caps Lock 打开,且按住shift键
){
$(".showhide").css("display","inline-block");
}
else{$(".showhide").css("display","none");}
}
document.getElementById('password').onkeypress = detectCapsLock;
</script>
获取鼠标在页面上的位置坐标:
function getCanvasPos(e){
var mouseX= e.offsetX;
var mouseY= e.offsetY;
var rect_left = mouseX;
var rect_top = mouseY;
return {
x: rect_left,
y: rect_top
};
}
求最小公倍数:
//两个数的最小公倍数
function gcd(a,b){
var minNum = Math.min(a,b),maxNum = Math.max(a,b),i=minNum,vper=0;
if(a ===0 || b===0){
return maxNum;
}
for(;i<=maxNum;i++){
vper = minNum * i;
if(vper % maxNum === 0){
return vper;
break;
}
}
}
//求一个数组的最下公倍数
function gcds(arr){
var onum = 0,i = 0,len = arr.length,midNum = 0;
for(;i<len;i++){
onum = Math.floor(arr[i]);//去掉小数
midNum = gcd(midNum,onum);
}
return midNum;
}