自己在平时项目中所使用到的JS代码

本文介绍了前端开发中几个实用的功能实现方法,包括从URL中获取参数、使用jQuery进行选项卡切换、处理时间戳以获得易读的时间格式、判断用户的手机操作系统类型以及禁用浏览器的右键菜单。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 ? '0' + 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()) + ':' + 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;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值