Skip to content

Instantly share code, notes, and snippets.

function jsonToString(obj){
var THIS = this;
switch(typeof(obj)){
case 'string':
return '"' + obj.replace(/(["\\])/g, '\\$1') + '"';
case 'array':
return '[' + obj.map(THIS.jsonToString).join(',') + ']';
case 'object':
if(obj instanceof Array){
var strArr = [];
function setKeyboardEvents() {
$(document).keydown(function(event) {
if(event.keyCode == 37){
doSomething();
}
else if (event.keyCode == 39){
doSomething();
}
});
}
<html>
<head>
<script type="text/javascript">
var count = 10;
function Picture()
{
if (event.wheelDelta >= 120)
Resize(++count);
else if (event.wheelDelta <= -120)
Resize(--count);
js判断是否为IE的办法
1.+[1,]
2.!+"\v1"
3.!!(window.attachEvent && navigator.userAgent.indexOf('Opera') === -1)
4.!!(!window.addEventListener&& navigator.userAgent.indexOf('Opera') === -1)
5.!!(document.all && navigator.userAgent.indexOf('Opera') === -1)
function preLoadImgs() {
var imgsUrl = [
'../qhome/img/progress_1.png','../qhome/img/progress_1_1.png',
'../qhome/img/progress_2.png','../qhome/img/progress_3.png',
'../qhome/img/progress_4.png'
];
for(var i in imgsUrl) {
preLoadImg(imgsUrl[i]);
}
}
//HTML、CSS禁止选择文字,针对IE、FF、Chrome等
<div unselectable="on" style="-moz-user-select:none;-webkit-user-select:none;" onselectstart="return false;">
你选不了我,
unselectable: IE/Opera,
-moz-user-select: FireFox,
onselectstart: IE/Safari,
-webkit-user-select:Chrome
</div>
@callblueday
callblueday / input 输入挂起事件
Created August 19, 2013 02:41
当用户在输入框中输入停顿一段时间后,会执行函数
$(".location").bind("input", function() {
var $this = $(this);
var delay =250; // delay after last input
clearTimeout($this.data('timer'));
$this.data('timer', setTimeout(function(){
$this.removeData('timer');
doSomething();
}, delay));
});
<A href="javascript:void(0)">click me</a>
//javascript:void(0) 不返回的意思
@callblueday
callblueday / form submit forbidden
Created August 19, 2013 01:34
return false is better because it prevents the default behavior of the event and also prevents the page from bubbling up. Note that this is only correct in jQuery event handlers.
//使用return false来禁用 form的默认submit事件。
<form method="POST" action="" id="search-form">
<input type="text" name="keywords" />
<input type="submit" value="Search" id="sButton" onclick="loadXMLDoc('file.xml')" />
</form>
<script>
window.onload = function() {
document.getElementById("search-form").onsubmit = function() {
第一种方法:
var timestamp = Date.parse(new Date());
结果:1280977330000
第二种方法:
var timestamp = (new Date()).valueOf();