首頁  >  文章  >  web前端  >  常用的js程式碼整理

常用的js程式碼整理

一个新手
一个新手原創
2017-09-09 15:14:022210瀏覽

1、取得網址列參數

function request(paras) {
    var url = location.search;
    var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
    var paraObj = {}
    for (i = 0; j = paraString[i]; i++) {
        paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=") + 1, j.length);
    };
    var returnValue = paraObj[paras.toLowerCase()];
    if (typeof(returnValue) == "undefined") {
        return "";
    } else {
        return returnValue;
    };
};

呼叫方法, var status = request('status'); 取得網址列中status的參數

2、jq,用索引值製作選項卡與內容之間的切換

function switch_tab(title, content) {
	title.first().addClass("on");
	content.first().show();
	title.click(function() {
		var a = $(this).index()
		if(content.eq(a).css("display") != "block") {
			content.hide(),
			content.eq(a).show(),
			title.removeClass("on"),
			$(this).addClass("on");
		};
	});
};

呼叫方法,switch_tab($('.title_box .title'),$('.content_box .box'))取得網址列中status的參數

3、js時間戳處理

// 传入时间戳。输出格式为:2017-05-14 00:08:46
common.pattern = function(data) {
    function replace(m) {
        return m < 10 ? &#39;0&#39; + m: m
    }
    var _date = new Date(parseInt(data));
    var re_date = replace(_date.getFullYear()) + "-" + replace(_date.getMonth() + 1) + "-" + replace(_date.getDate()) + " " + replace(_date.getHours()) + ":" + replace(_date.getMinutes()) + &#39;:&#39; + replace(_date.getSeconds());
    return re_date;
};

4、// 判斷手機系統安卓或ios

var ua = navigator.userAgent.toLowerCase();

if (/iphone|ipad|ipod/.test(ua)) {
    
} else if (/android/.test(ua)) {
   
};

5、阻止右鍵彈出查看代碼

// 阻止右键
document.body.onselectstart = document.body.oncontextmenu = function() {
    return false;
}

以上是常用的js程式碼整理的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn