当前位置: 首页 > Web前端 > HTML5

JS传参技巧总结

时间:2023-04-05 01:18:05 HTML5

1.隐式创建html标签这种方法一般配合ajax使用,上面的value使用模板引擎2.window['data']window["name"]="窗口对象";3、使用localStorage、cookies等存储window.localStorage.setItem("name","xiaoyueyue");window.localStorage.getItem("name");特性:cookie,localStorage,sessionStorage,indexDB特性cookielocalStoragesessionStorageindexDB数据生命周期一般由服务端生成,可以设置过期时间除非被清理,否则总会有页面关闭清理除非被清理,总会有一个数据存储大小为4K5M5MUnlimited每次与服务器的通信都会在header中携带,不会影响请求的性能,不参与,不参与,不参与。从上表可以看出,不再推荐存储cookies。如果不需要大量的数据存储,可以使用localStorage和sessionStorage。对于变化不大的数据,尽量使用localStorage存储,否则可以使用sessionStorage存储。注意:为了存储对象类型的数据,这种深拷贝方法会忽略函数和undefinedvarobj={type:undefined,text:"xiaoyueyue",methodd:function(){alert("我是methodd");}};localStorage.setItem("data",JSON.stringify(obj));console.log(JSON.parse(localStorage.getItem("data")));//{text:"xiaoyueyue"}4.获取地址栏方法自封装方法functionparseParam(url){varparamArr=decodeURI(url).split("?")[1].split("&"),obj={};for(vari=0;itest1test这个扩展了this传参的用法,在art-template的使用中想通了,不再需要只传一个id拼接成几个参数!快乐的!varbox=document.createElement("div");box.innerHTML="Click";document.body.appendChild(box);//name,age,friendfunctionalertInfo(data){alert("大家好,这里是"+data.name+",我今年"+data.age+"岁,我的好朋友是"+data.friend+"!");}这里需要注意一点:存储的数据——这里大写="的单词统一转为小写,例如:data-orderId="2a34fb64a13211e8a0f00050568b2fdd",此时实际值为this.dataset.orderid;既然event可以使用this,那么event中的event.target方法也是可以的:getcurrent根据类的索引值,参数可以是事件对象vargetIndexByClass=function(param){varelement=param.classname?param:param.target;varclassName=element.classname;vardomArr=Array.prototype.slice.call(document.querySelectorAll('.'+className));for(varindex=0;indexclickvarbtn=document.querySelector("button");btn.onclick=function(){alert(this.dataset.name);};7.字符串参数传单参数varname="xiaoyueyue",age=25;varbox=document.createElement("div");框。innerHTML="Click";document.body.appendChild(box);//name,agefunctionalertInfo(name,age,home,friend){alert("我是"+name);}多参数传递varname='xiaoyueyue',age='25',home='shanxi',friend='heizi',DQ=""";//双引号的HTMLvarparams="""+name+"",""+age+"",""+home+"",""+friend+""";varparams2=DQ+name+DQ+','+DQ+age+DQ+','+DQ+home+DQ+','+DQ+friend+DQ;varbox=document.createElement("div");box.innerHTML="点击";console.log(box)document.body.appendChild(box);//name,age,home,friendfunctionalertInfo(name,age,home,friend){alert("我是"+name+','+"我今年"+age+"岁了!")复杂传参vardata=[{name:"xiaoyueyue",age:"25",home:"shanxi",friend:"heizi"}];varbox=document.createElement("div"),html="";for(vari=0;i点击";}box.innerHTML=html;document.body.appendChild(box);functionalertInfo(id,name,age,home,friend){alert("我是"+name+","+friend+"ismygoodfriend");}8.argumentsarguments对象是所有(非箭头)函数中可用的局部变量,可以在函数中使用arguments对象来引用函数的参数。它是一个类似数组的对象。分配functionfenpei(){varargs=Array.prototype.slice.call(arguments);alert("我是"+args[2]+"油品,数量为"+args[1]+"吨,id为"+args[0]);}9.form表格使用form表格,ajax传递序列化参数//form表单序列化,摘自JSelevationfunctionserialize(form){varparts=[],field=null,i,len,j,optLen,option,optValue;对于(i=0,len=form.elements.length;i订单号:

送货方式:Delivery自取
document.querySelector("button").onclick=function(){console.log("param:"+serialize(document.getElementById("formData")));//参数:ordersaleCode=&extractType=0};10.发布订阅处理复杂逻辑传参支持先订阅后发布,先发布再订阅方法源码varEvent=(function(){varclientList={},pub,sub,remove;varcached={};sub=function(key,fn){if(!clientList[key]){clientList[k伊]=[];}//使用缓存执行的订阅不需要多次调用cached[key+"time"]==undefined?clientList[key].push(fn):"";if(cached[key]instanceofArray&&cached[key].length>0){//表示缓存的可以执行fn.apply(null,cached[key]);缓存[键+“时间”]=1;}};pub=function(){varkey=Array.prototype.shift.call(arguments),fns=clientList[key];if(!fns||fns.length===0){//初始默认缓存cached[key]=Array.prototype.slice.call(arguments,0);返回假;}for(vari=0,fn;(fn=fns[i++]);){//再次释放并更新缓存中的数据parametercached[key+"time"]!=undefined?(cached[key]=Array.prototype.slice.call(arguments,0)):"";fn.apply(这个,参数);}};remove=function(key,fn){varfns=clientList[key];//一起删除缓存的订阅varcachedFn=cached[key];如果(!fns&&!cachedFn){返回false;}if(!fn){fns&&(fns.length=0);cachedFn&&(cachedFn.length=0);}else{if(cachedFn){for(varm=cachedFn.length-1;m>=0;m--){var_fn_temp=cachedFn[m];如果(_fn_temp===fn){cachedFn.splice(m,1);}}}for(varn=fns.length-1;n>=0;n--){var_fn=fns[n];如果(_fn===fn){fns.splice(n,1);}}}};return{pub:pub,sub:sub,remove:remove};})();微信小程序中使用的例子:GlobalMountusing//app.jsApp({onLaunch:function(e){//注册存储,这是第二篇wx.Storage=Storage;//注册发布-订阅模式wx.yue=事件;}});useExample//在添加收货地址页面订阅onLoad:function(options){wx.yue.sub("addAddress",function(data){y.setData({addAddress:data})})}/***生命周期Function--监控页面隐藏*/onHide:function(){//取消冗余事件订阅wx.Storage.removeItem("addAddress");},onUnload:function(){//取消冗余事件订阅wx.是ue.remove("addAddress");}//通过地址页面获取数据并传递wx.yue.pub("addAddress",data.info);//补充跳转注意:注意卸载后使用数据,在页面关闭时操作扩展阅读深入理解JavaScript参数传递