分享javascript、jquery实用代码段

时间:2021-05-25

本文实例为大家简单分享javascript、jquery实用demo,供大家参考,具体内容如下

javascript判断H5页面离开

function onbeforeunloadFn(){ console.log('离开页面'); //...code}function showOnbeforeunload(flags){ if(flags){ document.body.onbeforeunload = onbeforeunloadFn; }else{ document.body.onbeforeunload = null; }}$(function(){ showOnbeforeunload(true);})

jq判断img标签图片地址是否已经加载完毕

imgStatus = 0; $("img").each(function(){ if(this.complete){ imgStatus++; } });

iframe获取内容-和设置

if($(".ad_show iframe").size() > 0 ){ $(".ad_show iframe").attr("id","iframead"); var iframebox = document.getElementById("iframead").contentWindow; iframebox.document.body.innerText = "1234";}

javascript获取浏览器上一页的url

var beforeUrl = document.referrer;

关于头疼的移动端点击冒泡事件

<script>$(".class").live('tap',function(oEvent){ e = window.event || oEvent; if(e.stopPropagation){ e.stopPropagation(); }else{ e.cancelBubble = true; } e.preventDefault();});</script><!--html--><div class="parentTap" data-flag="true"> <div class="childTap" data-flag="false"> <div class="childsTap" data-flag="false"> .... </div> </div></div><!--给父级parentTap绑定一个点击事件--><!--给子级childTap绑定一个点击事件--><!--给子孙级childsTap绑定一个点击事件--><script type="text/javascript"> var bindInit = function(className){ if($(className).size() > 0){ $(className).on('tap',function(oEvent){ e = window.event || oEvent;if(e.stopPropagation){e.stopPropagation();}else{e.cancelBubble = true;}e.preventDefault(); var flag = $(this).data('flag'); if(flag){ } }); } } $(function(){ bindInit('.parentTap'); bindInit('.childTap'); bindInit('.childsTap'); });</script>

简单倒计时功能

<div class="newTime" data-end="2016-10-13 23:59:59" data-now="2016-10-13 03:59:59"> <div class="t_d"></div> <div class="t_h"></div> <div class="t_m"></div> <div class="t_s"></div></div> <script type="text/javascript"> var timeDown = { GetRTime: function (timeId,oldNowTime) { var tempTime; if(oldNowTime){ tempTime = new Date(oldNowTime.getTime() + 1000); } var EndTime = new Date($("#" + timeId).data("end")); if (!tempTime) { if ($("#" + timeId).data("now") == "" || $("#" + timeId).data("now") == "null") { var NowTime = new Date(); } else { var NowTime = new Date($("#" + timeId).data("now")); } } else { var NowTime = tempTime; } if (EndTime.getTime() >= NowTime.getTime()) { var t = EndTime.getTime() - NowTime.getTime(); var d = Math.floor(t / 1000 / 60 / 60 / 24); var h = Math.floor(t / 1000 / 60 / 60 % 24); var m = Math.floor(t / 1000 / 60 % 60); var s = Math.floor(t / 1000 % 60); $(".t_d", "#" + timeId).html((d > 9 ? '' : '0') + d); $(".t_h", "#" + timeId).html((h > 9 ? '' : '0') + h); $(".t_m", "#" + timeId).html((m > 9 ? '' : '0') + m); $(".t_s", "#" + timeId).html((s > 9 ? '' : '0') + s); tempTime = new Date(NowTime.getTime() + 1000); setTimeout(function () { timeDown.GetRTime(timeId,NowTime); }, 1000); } else if (EndTime.getTime() == NowTime.getTime()) { $("#"+timeId).hide(); } } } var t=0; if ($(".newTime").size() > 0) { $(".newTime").each(function(){ var timeId="timeOut"+t; $(this).attr("id",timeId); t++; timeDown.GetRTime(timeId,null); }); } </script>

jQuery的节点操作

jQuery.parent(expr) jQuery.parents(expr) jQuery.children(expr) jQuery.contents()

js中if判断语句中的in语法

if(1 == 1){ alert("1等于1");}else{ alert("1不等于1");}if(1 in window){ alert("window包含1");}else{ alert("window不包含1");}

js的try-catch

try{ foo.bar();}catch(e){ console.log(e.name + ":" + e.message);}try{ throw new Error("Whoops!");}catch(e){ console.log(e.name + ":" + e.message);} try { coo.bar();//捕获异常,ReferenceError:引用无效的引用}catch(e){ console.log(e instanceof EvalError); console.log(e instanceof RangeError); if(e instanceof EvalError){ console.log(e.name + ":" + e.message); }else if(e instanceof RangeError){ console.log(e.name + ":" + e.message); }else if(e instanceof ReferenceError){ console.log(e.name + ":" + e.message); }}

js中typeof和instanceof区别

try { throw new myBlur(); // 抛出当前对象 }catch(e){ console.log(typeof(e.a)); //返回function类型 if(e.a instanceof Function){//instanceof用于判断一个变量是否某个对象的实例,true console.log("是一个function方法"); e.a();//执行这个方法,输出"失去焦点" }else{ console.log("不是一个function方法"); }}function myBlur(){ this.a = function(){ console.log("失去焦点"); };} if(typeof(param) == "object"){ alert("该参数等于object类型"); }else{ alert("该参数不等于object类型"); }console.log(Object instanceof Object);//trueconsole.log(Function instanceof Function);//true console.log(Number instanceof Number);//falseconsole.log(String instanceof String);//falseconsole.log(Function instanceof Object);//trueconsole.log(Foo instanceof Function);//trueconsole.log(Foo instanceof Foo);//false

HTML5缓存sessionStorage

sessionStorage.getItem(key)//获取指定key本地存储的值sessionStorage.setItem(key,value)//将value存储到key字段sessionStorage.removeItem(key)//删除指定key本地存储的值sessionStorage.length//sessionStorage的项目数

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章