• 每天进步一点点!

文章分类

推荐网站

常用手册

原生JS设置和获取cookie【原创】

function setCookie(name, value) { var t = new Date(); t.setTime(t.getTime() + 30 * 24 * 3600 * 1000); document.cookie = escape(name) + "=" + escape(value) + ";path=/;expires=" + t.toGMTString(); }
function getCookie(name) { if (new RegExp("\\b" + name + "=([^;]+)", "g").test(document.cookie)) return unescape(RegExp.$1 || ''); return ''; }

 

类别:JavaScript | 浏览(90) | 评论(0) | 阅读全文>>

jquery操作select选中时火狐不起作用【原创】

今天遇到一个很有意思的问题,用jquery操作select列表框,动态将某个option的selected属性赋为"selected",在火狐和chrom下都能实现其赋值,但是在火狐下select列表框的文本并没有变化,在chrome下是有变化的,chrome下是正确的结果,也是我想要的结果。网上查了一下,原来处理让option选中的方法有问题。

html代码

<select id="dict1">
    <option class="dict1_all" value="0">全部</option>
    <option value="1">root</option>
    <option value="8">root1</option>
    <option value="9">root2</option>
</select>
<select id="dict2">
    <option class="dict2_all" value="0">全部</option>
    <option style="display: none;" class="fid_1" value="2">子类1</option>
    <option style="display: none;" class="fid_1" value="3">子类2</option>
    <option style="" class="fid_8" value="4">子类3</option>
</select>

 

JS代码

$('#dict1').change(function() {
    var dictId = $(this).val();
    //下面这种方法,再FF下赋值是成功的,但是select列表框的文本不会变化,在chrome下会变化
    //$('#dict2 .dict2_all').attr('selected', 'true'); 
    //下面这种方法,可以使select列表框的文本发生变化
    $('#dict2').val(0);
    if(dictId !== 0) {
        $('#dict2').children().not('.dict2_all').hide();
        $('#dict2 .fid_' + dictId).show();  
    }
});

 

类别:JavaScript | 浏览(99) | 评论(0) | 阅读全文>>

JS回车出发事件【原创】

document.onkeydown = function(e){
    var ev = document.all ? window.event : e;
    if(ev.keyCode==13) {
        $("#search_img").trigger('click');
    }
}

 

类别:JavaScript | 浏览(82) | 评论(0) | 阅读全文>>

js判断IE浏览器的版本号【原创】

if(navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion .split(";")[1].replace(/[ ]/g,"")=="MSIE6.0"){   
    alert("IE 6.0");   
}else if(navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion .split(";")[1].replace(/[ ]/g,"")=="MSIE7.0"){   
    alert("IE 7.0");   
}else if(navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion .split(";")[1].replace(/[ ]/g,"")=="MSIE8.0"){   
    alert("IE 8.0");   
}else if(navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion .split(";")[1].replace(/[ ]/g,"")=="MSIE9.0"){   
    alert("IE 9.0");   
}

 

类别:JavaScript | 浏览(89) | 评论(0) | 阅读全文>>

JS日期和时间戳相互转换【原创】

今天遇到一个关于JS的日期和时间戳相互转换的问题,在网上找了许久略有发现,自己写了两个方法,记录一下。

 

/**
 * 返回给定时间部件的时间戳
 */
function dateToTimestamp(year, month, day, hours, minutes, seconds) {
	var date = new Date();
	year = year.length == 0 ? date.getFullYear() : year;
	month = month.length == 0 ? date.getMonth() + 1 : month + 1;
	day = day.length == 0 ? date.getDate() : day;
	hours = hours.length == 0 ? date.getHours() : hours;
	minutes = minutes.length == 0 ? date.getMinutes() : minutes;
	seconds = seconds.length == 0 ? date.getSeconds() : seconds;
	// console.log('year = ' + year + ' month = ' + month + ' day =' + day + ' hours = ' + hours + ' minutes=' + minutes + ' seconds = ' + seconds );
	return Date.UTC(year, month, day, hours, minutes, seconds) / 1000 - 3600 *  8
}

/**
 * 返回给定时间戳对应的日期格式
 */
function timestampToDate(timestamp, dateDelimiter, timeDelimiter) {
	var date = new Date();
	timestamp = timestamp == undefined ?  date.getTime() : timestamp;
	dateDelimiter = dateDelimiter == undefined ? '-' : dateDelimiter;
	timeDelimiter = timeDelimiter== undefined ?  ':' : timeDelimiter;
	
	var newDate = new Date(parseInt(timestamp) * 1000);
	var year = newDate.getFullYear();
	var month = newDate.getMonth() < 10 ? '0' + newDate.getMonth() : newDate.getMonth();
	var day = newDate.getDate() < 10 ? '0' + newDate.getDate() : newDate.getDate();
	var hours = newDate.getHours() < 10 ? '0' +newDate.getHours() : newDate.getHours();
	var minutes = newDate.getMinutes() < 10 ? '0' + newDate.getMinutes() : newDate.getMinutes();
	var seconds = newDate.getSeconds() < 10 ? '0' + newDate.getSeconds() : newDate.getSeconds();
	var fomateDateTime = year + ''+ dateDelimiter + '' + month + '' + dateDelimiter +''+ day + ' ' + hours + '' +  timeDelimiter + '' + minutes +'' + timeDelimiter + '' + seconds
	// console.log('fomateDateTime = ' +fomateDateTime);
	return fomateDateTime;
}

 

类别:JavaScript | 浏览(96) | 评论(0) | 阅读全文>>