禁止右键和复制
$(document).ready( function() { document.body.oncontextmenu = document.body.ondragstart = document.body.onselectstart = document.body.onbeforecopy = function() { return false; }; document.body.onselect = document.body.oncopy =document.body.onmouseup = function() { document.selection.empty(); }; });
JSON的字符串解析成JSON数据格式
//1、 eval() -- 计算javascript字符串 var dataObj=eval("("+data+")");//转换为json对象 alert(eval("{}"); // return undefined alert(eval("({})");// return object[Object] //2、使用Function对象来进行返回解析,典型应用就是在JQUERY中的AJAX方法下的success等对于返回数据data的解析 var json='{"name":"CJ","age":18}'; data =(new Function("","return "+json))();
js页面跳转
1、window.open()
function goOutSystem(outSystemSign){ window.open('http://jxjy.cdeledu.com/cdel_jxjy/'+outSystemSign+'.shtml'); }
2、window.location.href
<script language="JavaScript" type="text/javascript"> window.location.href="target.aspx"; </script>
3、window.navigate
<script language="javascript"> window.navigate("target.aspx"); </script>
4、window.loction.replace(注意跟第一种方式的区别)
<script language="javascript"> window.location.replace("target.aspx"); </script>
进系统默认的是1.aspx,进入2.aspx时,用window.location.replace("3.aspx");与window.location.href ("3.aspx");用户界面效果无区别,但当3.aspx调用window.history.Go(-1); window.history.back();时,使用前者的话返回方法不好用,会返回到1.aspx。
5、self.location,和下面的top.location有小小区别
<script language="JavaScript"> self.location='target.aspx'; </script>
6、top.location
<script language="javascript"> top.location='target.aspx'; </script>
7、$(window).attr('location',_ctx+"/app/interface/appLoad");
8、不推荐这种方式跳转
<script language="javascript"> alert("返回"); window.history.back(-1); </script>
9、meta方式实现跳转(content = 3 单位是秒)
<meta http-equiv=refresh content=20;URL="http://www.wyxg.com"> //2隔20秒后跳转到http://www.wyxg.com页面
区分null、0、undefined、false
typeof(undefined) == 'undefined'
typeof(null) == 'object'
typeof("") == 'string'
typeof(0) == 'number'
typeof(false) == 'boolean'
这五个值的共同点是,在if语句中做判断,都会执行false分支。当然从广义上来看,是说明这些数值都是其对应数据类型上的无效值或空值。还有这五个值作!运算,结果全为:true。
它们到String的转换关系是:
- String(undefined) -> "undefined"
- String(null) -> "null"
- String("") -> ""
- String(0) -> "0"
- String(false) -> "false"
for (var i = 0; i < data.rows.length; i++) { var status = -1; //-1无意义,只是不影响状态的判断 if(typeof(data.rows[i].invoiceStatus) == 'number'){ status = data.rows[i].invoiceStatus; if (status == 0 || status == 1 || status == 3 || status == 6 || status == 7 ||status == 8 || status == 9 ||
status == 12||status == 13 || status == 14) { $("input[type='checkbox']")[i + 1].disabled = true; } } }
获取当前日期时间“yyyy-MM-dd HH:MM:SS”
//法一 function getNowFormatDate() { var date = new Date(); var seperator1 = "-"; var seperator2 = ":"; var month = date.getMonth() + 1; var strDate = date.getDate(); if(month >= 1 && month <= 9) { month = "0" + month; } if(strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + date.getHours() + seperator2 + date.getMinutes() +seperator2 + date.getSeconds(); document.getElementById("info1").innerHTML = currentdate; return currentdate; }
//法二 function time() { var now = new Date(); var year = now.getFullYear(); var month = now.getMonth(); var date = now.getDate(); var hour = now.getHours(); var minite = now.getMinutes(); var second = now.getSeconds(); var seperator1 = "-"; var seperator2 = ":"; document.getElementById("info1").innerHTML = year + seperator1 + (month + 1) + seperator1 + date + " " + hour +
seperator2 + minite + seperator2 + second;
}
复制到剪切板(app)
function codeCopy() { if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) {//区分iPhone设备 window.getSelection().removeAllRanges();//这段代码必须放在前面否则无效 var Url2=document.getElementById("groomCode");//要复制文字的节点 var range = document.createRange(); range.selectNode(Url2);// 选中需要复制的节点 window.getSelection().addRange(range);// 执行选中元素 var successful = document.execCommand('copy');// 执行 copy 操作 window.getSelection().removeAllRanges();// 移除选中的元素 popTxt('复制成功'); }else{ var code=document.getElementById("groomCode").innerText; var input = document.createElement("input"); input.value = code; document.body.appendChild(input); input.select(); input.setSelectionRange(0, input.value.length), document.execCommand('Copy'); document.body.removeChild(input); popTxt('复制成功'); } }
复制到剪切板(pc)
function copy(v) { var code = $("#andIOSEntSignAddress").val();//要复制文字 var input = document.createElement("input"); input.value = code; document.body.appendChild(input); input.select(); input.setSelectionRange(0, input.value.length), document.execCommand('Copy'); document.body.removeChild(input); layer.msg("复制成功", {icon : 1}); }
判断是否是移动设备打开,判断ios系统还是android系统
var browser = { versions: function () { var u = navigator.userAgent, app = navigator.appVersion; return { //移动终端浏览器版本信息 trident: u.indexOf('Trident') > -1, //IE内核 presto: u.indexOf('Presto') > -1, //opera内核 webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核 gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核 mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端 ios: !!u.match(/(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端 android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或uc浏览器 iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器 iPad: u.indexOf('iPad') > -1, //是否iPad webApp: u.indexOf('Safari') == -1 //是否web应该程序,没有头部与底部 }; }(), language: (navigator.browserLanguage || navigator.language).toLowerCase() } if (browser.versions.mobile) {//判断是否是移动设备打开 var ua = window.navigator.userAgent.toLowerCase(); if (ua.match(/MicroMessenger/i) == "micromessenger") {//在微信中打开 if(ua.indexOf("android")!=-1){
//安卓系统 }else {
//ios系统 } } if (ua.match(/WeiBo/i) == "weibo") {
//在新浪微博客户端打开 } if (browser.versions.ios) { //是否在IOS浏览器打开 } if(browser.versions.android){ //是否在安卓浏览器打开 } } else { //否则就是PC浏览器打开 }
微信内置浏览器浏览H5页面弹出的键盘遮盖文本框的解决办法
window.addEventListener("resize", function () { var widthBody = document.body.clientWidth var heightBody = widthBody*1.77 if (document.activeElement.tagName == "INPUT" || document.activeElement.tagName == "TEXTAREA") { window.setTimeout(function () { $('body').css("height",heightBody) document.activeElement.scrollIntoViewIfNeeded(); }, 0); }else{ $('body').css("height","100%") } });
刷新页面
- history.go(0)
- location.reload()
- location=location
- location.assign(location)
- document.execCommand('Refresh')
- window.navigate(location)
- location.replace(location)
- document.URL=location.href
//定时刷新 $(document ).ready(function(){ searchData(); setTimeout('myRefresh()', 2000);//2秒刷新一次 }); function myRefresh() { //window.location.reload(); searchData(); $.getJSON("<c:url value='/signup/isSignupComplete.do'/>",{ ranNum : Math.random()}, function(result){ if(result.totalNum == 0){ setTimeout('myRefresh()', 2000);//2秒刷新一次 } else if(result.totalNum == 1){ window.location.href="http://www.baidu.com"; } }); } //自动刷新 1.把如下代码加入<head>区域中 <meta http-equiv="refresh" content="20"> //每隔20秒刷新一次页面. 2.页面自动刷新js版 <mce:script language="JavaScript"> function myrefresh(){ window.location.reload(); } setTimeout('myrefresh()',1000); //指定1秒刷新一次 </mce:script>
获取data-*属性值
//需要获取的就是data-id 和 dtat-vice-id的值 <li id="getId" data-id="122" data-vice-id="11">获取id</li>
一:getAttribute()方法
const getId = document.getElementById('getId'); //getAttribute()取值属性 console.log(getId.getAttribute("data-id"));//122 console.log(getId.getAttribute("data-vice-id"));//11 //setAttribute()赋值属性 getId.setAttribute("data-id","48"); console.log(getId.getAttribute("data-id"));//48
二:dataset()方法
//data-前缀属性可以在JS中通过dataset取值,更加方便 console.log(getId.dataset.id);//112 //data-vice-id连接取值使用驼峰命名法取值 console.log(getId.dataset.viceId);//11 //赋值 getId.dataset.id = "113";//113 getId.dataset.viceId--;//10 //新增data属性 getId.dataset.id2 = "100";//100 //删除,设置成null,或者delete getId.dataset.id2 = null;//null delete getId.dataset.id2;//undefind
三:jquery data()方法
var id = $("#getId").data("id"); //122 var viceId = $("#getId").data("vice-id"); //11 //赋值 $("#getId").data("id","100");//100
四:jquery attr()方法
var id = $("#getId").attr("data-id"); //122 var viceId = $("#getId").attr("data-vice-id"); //11 //赋值 $("#getId").attr("data-id","100");//100