Js传参技巧总结

1.隐式创建 html 标签

1
<input type="hidden" name="tc_id" value="{{tc_id}}">

这种方法一般配合ajax,上面的value使用了模板引擎

2.window[‘data’]

1
window['name'] = "the window object";

3.使用localStorage,cookie等存储

1
2
window.localStorage.setItem("name", "xiaoyueyue");
window.localStorage.getItem("name")

特点

1、localStorage 是持久存储,不主动删除 一直存在  
   sessionStorage 是临时存储,关闭浏览器数据就没了
2、localStorage 可以多窗口共享
   sessionStorage 不能多窗口共享数据

注意点:存储object类型数据

1
2
3
4
5
6
7
var obj = {
type:'name',
text:'xiaoyueyue'
}

localStorage.setItem('data',JSON.stringify(obj));
console.log(JSON.parse(localStorage.getItem('data')));

4.获取地址栏方法

  1. 自己封装的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.正则表达式方法

1
2
3
4
5
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.标签绑定函数传参

1
2
3
4
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!

1
2
3
4
5
6
7
8
9
10
    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对象

1
2
3
4
5
6
7
8
9
10
11
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-* 自定义属性

1
<button data-name="xiaoyueyue">点击</button>
1
2
3
4
var btn = document.querySelector("button")
btn.onclick = function () {
alert(this.dataset.name)
}

7.字符串传参

单个参数

1
2
3
4
5
6
7
8
9
10
11
12
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)
}

多参传递

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 var name = 'xiaoyueyue',
age = '25',
home = 'shanxi',
friend = 'heizi';

var params = "&quot;" + name + "&quot;,&quot;" + age + "&quot;,&quot;" + home + "&quot;,&quot;" + friend + "&quot;";
var box = document.createElement("div");
box.innerHTML = "<button onclick='alertInfo(" + params + ")'>点击</button>";
document.body.appendChild(box);


// name, age,home,friend
function alertInfo(name, age, home, friend) {
alert("我是" + name + ',' + "我今年" + age + "岁了!")
}

复杂传参

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
var data = [
{
"name": "xiaoyueyue",
"age": "25",
"home": "shanxi",
"friend": "heizi"
}

]

var box = document.createElement("div");

for (var i = 0; i < data.length; i++) {
box.innerHTML = "<button id='btn' onclick='alertInfo(id,\"" + data[i].name + "\",\"" + data[i].age + "\",\"" + data[i].home + "\",\"" + data[i].friend + "\")'>点击</button>";
}

document.body.appendChild(box);

function alertInfo(id, name, age, home, friend) {
alert("我是" + name + "," + friend + "是我的好朋友")
}

8.arguments

arguments对象是所有(非箭头)函数中都可用的局部变量。你可以使用arguments对象在函数中引用函数的参数。它是一个类数组的对象。

1
<button onclick="fenpei('f233c7a290ae11e8a0f00050568b2fdd','100','0号 车用柴油(Ⅴ)')">分配</button>

1
2
3
4
5
function fenpei() {
var args = Array.prototype.slice.call(arguments);
alert("我是" + args[2] + "油品,数量为 " + args[1] + " 吨, id为 " + args[0])

}

9.form表单

借助form表单,ajax传递序列化参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// 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("&");
}

栗子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<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>

1
2
3
document.querySelector("button").onclick = function () {
console.log('param: '+serialize(document.getElementById("formData"))); // param: ordersaleCode=&extractType=0
}