1.隐式建立 html 标签
<input type="hidden" name="tc_id" value="{{tc_id}}">
这类要领平常合营 ajax,上面的 value 运用了模板引擎
2.window[‘data’]
window["name"] = "the window object";
3.运用 localStorage,cookie 等存储
window.localStorage.setItem("name", "xiaoyueyue");
window.localStorage.getItem("name");
特征:cookie,localStorage,sessionStorage,indexDB
特征 | cookie | localStorage | sessionStorage | indexDB |
---|---|---|---|---|
数据性命周期 | 平常由服务器天生,能够设置逾期时刻 | 除非被清算,不然一向存在 | 页面封闭就清算 | 除非被清算,不然一向存在 |
数据存储大小 | 4K | 5M | 5M | 无穷 |
与服务端通讯 | 每次都邑携带在 header 中,关于要求机能影响 | 不介入 | 不介入 | 不介入 |
从上表能够看到,cookie
已不发起用于存储。假如没有大批数据存储需求的话,能够运用 localStorage
和 sessionStorage
。关于不怎么转变的数据只管运用 localStorage
存储,不然能够用 sessionStorage
存储。
注重点:存储
object
范例数据,此深拷贝要领会疏忽掉函数和
undefined
var obj = {
type: undefined,
text: "xiaoyueyue",
methord: function() {
alert("I am an methord");
}
};
localStorage.setItem("data", JSON.stringify(obj));
console.log(JSON.parse(localStorage.getItem("data"))); // {text: "xiaoyueyue"}
4.猎取地点栏要领
- 本身封装的要领
function parseParam(url) {
var paramArr = decodeURI(url)
.split("?")[1]
.split("&"),
obj = {};
for (var i = 0; i < paramArr.length; i++) {
var item = paramArr[i];
if (item.indexOf("=") != -1) {
var tmp = item.split("=");
obj[tmp[0]] = tmp[1];
} else {
obj[item] = true;
}
}
return obj;
}
2.正则表达式要领
function GetQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]);
return null;
}
5.标签绑定函数传参
<!--base-->
<button id="test1" onclick="alert(id)">test1</button>
<!--高等-->
<button
id="test"
name="123"
yue="xiaoyueyue"
friend="heizi"
onclick="console.log(this.getAttribute('yue'),this.getAttribute('friend'))"
>
test
</button>
this 拓展
运用 this 传参,在运用 art-template 中揣摩出来的,再也不必只通报一个 id 拼接成好几个参数了!happy!
var box = document.createElement("div");
box.innerHTML =
"<button id='1' data-name='xiaoyueyue' data-age='25' data-friend='heizi' onclick='alertInfo(this.dataset)'>点击</button>";
document.body.appendChild(box);
// name,age,friend
function alertInfo(data) {
alert(
"大家好,我是" +
data.name +
", 我本年" +
data.age +
"岁了,我的好朋友是" +
data.friend +
" !"
);
}
这里须要注重一点:存储的 data—含有大写的单词 =》这里会一致转化为小写,比方:data-orderId = “2a34fb64a13211e8a0f00050568b2fdd”,在现实取值的时刻为
this.dataset.orderid
;
event
既然能够运用 this,那末在事宜当中event.target
要领也是能够的:
依据 class 猎取当前的索引值,参数能够为 event 对象
var getIndexByClass = function (param) {
var element = param.classname ? param : param.target;
var className = element.classname;
var domArr = Array.prototype.slice.call(document.querySelectorAll('.' + className));
for (var index = 0; index < domArr.length; index++) {
if (domArr[index] === element) {
return index;
}
}
return -1;
},
6.HTML5 data-* 自定义属性
<button data-name="xiaoyueyue">点击</button>
var btn = document.querySelector("button");
btn.onclick = function() {
alert(this.dataset.name);
};
7.字符串传参
单个参数
var name = "xiaoyueyue",
age = 25;
var box = document.createElement("div");
box.innerHTML = "<button onclick=\"alertInfo('" + name + "')\">点击</button>";
document.body.appendChild(box);
// name, age
function alertInfo(name, age, home, friend) {
alert("我是" + name);
}
多参通报
var name = 'xiaoyueyue',
age = '25',
home = 'shanxi',
friend = 'heizi',
DQ = """; // 双引号的超文本标记言语
var params = """ + name + "","" + age + "","" + home + "","" + friend + """;
var params2 = DQ + name + DQ + ',' + DQ + age + DQ + ',' + DQ + home + DQ + ',' + DQ + friend + DQ;
var box = document.createElement("div");
box.innerHTML = "<button onclick='alertInfo(" + params + ")'>点击</button>";
console.log(box)
document.body.appendChild(box);
// name, age,home,friend
function alertInfo(name, age, home, friend) {
alert("我是" + name + ',' + "我本年" + age + "岁了!")
庞杂传参
var data = [
{
name: "xiaoyueyue",
age: "25",
home: "shanxi",
friend: "heizi"
}
];
var box = document.createElement("div"),
html = "";
for (var i = 0; i < data.length; i++) {
html +=
"<button id='btn' onclick='alertInfo(id,\"" +
data[i].name +
'","' +
data[i].age +
'","' +
data[i].home +
'","' +
data[i].friend +
"\")'>点击</button>";
}
box.innerHTML = html;
document.body.appendChild(box);
function alertInfo(id, name, age, home, friend) {
alert("我是 " + name + " , " + friend + " 是我的好朋友");
}
8.arguments
arguments
对象是一切(非箭头)函数中都可用的局部变量。你能够运用 arguments 对象在函数中援用函数的参数。它是一个类数组的对象。
<button
onclick="fenpei('f233c7a290ae11e8a0f00050568b2fdd','100','0号 车用柴油(Ⅴ)')"
>
分派
</button>
function fenpei() {
var args = Array.prototype.slice.call(arguments);
alert("我是" + args[2] + "油品,数目为 " + args[1] + " 吨, id为 " + args[0]);
}
9.form 表单
借助form
表单,ajax 通报序列化参数
// form表单序列化,摘自JS高程
function serialize(form) {
var parts = [],
field = null,
i,
len,
j,
optLen,
option,
optValue;
for (i = 0, len = form.elements.length; i < len; i++) {
field = form.elements[i];
switch (field.type) {
case "select-one":
case "select-multiple":
if (field.name.length) {
for (j = 0, optLen = field.options.length; j < optLen; j++) {
option = field.options[j];
if (option.selected) {
optValue = "";
if (option.hasAttribute) {
optValue = option.hasAttribute("value")
? option.value
: option.text;
} else {
optValue = option.attributes["value"].specified
? option.value
: option.text;
}
parts.push(
encodeURIComponent(field.name) +
"=" +
encodeURIComponent(optValue)
);
}
}
}
break;
case undefined: //fieldset
case "file": //file input
case "submit": //submit button
case "reset": //reset button
case "button": //custom button
break;
case "radio": //radio button
case "checkbox": //checkbox
if (!field.checked) {
break;
}
/* falls through */
default:
//don't include form fields without names
if (field.name.length) {
parts.push(
encodeURIComponent(field.name) +
"=" +
encodeURIComponent(field.value)
);
}
}
}
return parts.join("&");
}
栗子:
<form id="formData">
<div class="pop-info">
<label for="ordersaleCode">定单编号:</label>
<input
type="text"
id="ordersaleCode"
name="ordersaleCode"
placeholder="请输入定单编号"
/>
</div>
<div class="pop-info">
<label for="extractType">配送体式格局:</label>
<select id="extractType" name="extractType" class="mySelect">
<option value="0" selected>配送</option>
<option value="1">自提</option>
</select>
</div>
</form>
<button>猎取参数</button>
document.querySelector("button").onclick = function() {
console.log("param: " + serialize(document.getElementById("formData"))); // param: ordersaleCode=&extractType=0
};
10. 宣布定阅处置惩罚庞杂逻辑传参
支撑先定阅后宣布,以及先宣布后定阅
- 要领源码
var Event = (function() {
var clientList = {},
pub,
sub,
remove;
var cached = {};
sub = function(key, fn) {
if (!clientList[key]) {
clientList[key] = [];
}
// 运用缓存实行的定阅不必屡次挪用实行
cached[key + "time"] == undefined ? clientList[key].push(fn) : "";
if (cached[key] instanceof Array && cached[key].length > 0) {
//申明有缓存的 能够实行
fn.apply(null, cached[key]);
cached[key + "time"] = 1;
}
};
pub = function() {
var key = Array.prototype.shift.call(arguments),
fns = clientList[key];
if (!fns || fns.length === 0) {
//初始默许缓存
cached[key] = Array.prototype.slice.call(arguments, 0);
return false;
}
for (var i = 0, fn; (fn = fns[i++]); ) {
// 再次宣布更新缓存中的 data 参数
cached[key + "time"] != undefined
? (cached[key] = Array.prototype.slice.call(arguments, 0))
: "";
fn.apply(this, arguments);
}
};
remove = function(key, fn) {
var fns = clientList[key];
// 缓存定阅一并删除
var cachedFn = cached[key];
if (!fns && !cachedFn) {
return false;
}
if (!fn) {
fns && (fns.length = 0);
cachedFn && (cachedFn.length = 0);
} else {
if (cachedFn) {
for (var m = cachedFn.length - 1; m >= 0; m--) {
var _fn_temp = cachedFn[m];
if (_fn_temp === fn) {
cachedFn.splice(m, 1);
}
}
}
for (var n = fns.length - 1; n >= 0; n--) {
var _fn = fns[n];
if (_fn === fn) {
fns.splice(n, 1);
}
}
}
};
return {
pub: pub,
sub: sub,
remove: remove
};
})();
在微信小顺序中运用的例子:
- 全局挂载运用
// app.js
App({
onLaunch: function(e) {
// 注册 storage,这是第二条
wx.Storage = Storage;
// 注册宣布定阅形式
wx.yue = Event;
}
});
- 运用实例
// 增加收货地点页面定阅
onLoad: function (options) {
wx.yue.sub("addAddress", function (data) {
y.setData({
addAddress: data
})
})
}
/**
* 性命周期函数--监听页面隐蔽
*/
onHide: function () {
// 作废过剩的事宜定阅
wx.Storage.removeItem("addAddress");
},
onUnload: function () {
// 作废过剩的事宜定阅
wx.yue.remove("addAddress");
}
// 通报地点页面猎取好数据通报
wx.yue.pub("addAddress", data.info);
// 补充跳转返回
注重:运用完成数据后要注重卸载,在页面被封闭时操纵