栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

分享javascript、jquery实用代码段

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

分享javascript、jquery实用代码段

本文实例为大家简单分享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;

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






 
 
  ....
 
 




简单倒计时功能


 
 
 
 

 

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);//true
console.log(Function instanceof Function);//true 
console.log(Number instanceof Number);//false
console.log(String instanceof String);//false
console.log(Function instanceof Object);//true
console.log(Foo instanceof Function);//true
console.log(Foo instanceof Foo);//false

HTML5缓存sessionStorage

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


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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/93373.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号