首页  >  文章  >  web前端  >  常用的js代码整理

常用的js代码整理

一个新手
一个新手原创
2017-09-09 15:14:022211浏览

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