MXOXW

Life always finds a way.

JavaScript常用函数汇总

| Comments

获取URL参数

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* 获取URL参数
* @param {[string]} name [description]
* @param {[string]} url [description]
* @return {[string]} [description]
*/

function getQuery(name, url) {
//参数:变量名,url为空则表从当前页面的url中取
var u = arguments[1] || window.location.search,
reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"),
r = u.substr(u.indexOf("\?") + 1).match(reg);
return r != null ? r[2] : "";
}

事件绑定

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
/**
* 事件绑定
*/

var EventUtil = {
addHandler: function( element, type, handler ) {
if ( element.addEventListener ) {
element.addEventListener ( type, handler, false );
} else if ( element.attachEvent ) {
element.attachEvent ( "on" + type, handler );
} else {
element["on" + type] = handler;
}
},

getEvent: function ( event ) {
return event ? event : window.event;
},

getTarget: function ( event ) {
return event.target || event.srcElement;
},

preventDefault: function ( event ) {
if ( event.preventDefault ) {
event.preventDefault();
} else {
event.returnValue = false;
}
},

removeHandler: function( element, type, handler ) {
if ( element.removeEventListener ) {
element.removeEventListener ( type, handler, false );
} else if ( element.detachEvent ) {
element.detachEvent ( "on" + type, handler );
} else {
element["on" + type] = null;
}
},

stopPropagation: function ( event ) {
if ( event.stopPropagation ) {
event.stopPropagation();
} else {
event.cancelBubble = true;
}
}

};

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
/**
* 对象转为URL参数
* @param {[type]} param [description]
* @param {[type]} key [description]
* @return {[type]} [description]
*/

var parseParam = function(param, key) {
var paramStr = "";
if (param instanceof String || param instanceof Number || param instanceof Boolean) {
paramStr += "&" + key + "=" + encodeURIComponent(param);
} else {
$.each(param, function(i) {
var k = key == null ? i : key + (param instanceof Array ? "[" + i + "]" : "." + i);
paramStr += '&' + parseParam(this, k);
});
}
return paramStr.substr(1);
};

//调用:
var obj = {
name: 'tom',
'class': {
className: 'class1'
},
classMates: [{
name: 'lily'
}]
};
parseParam(obj);
结果: "name=tom&class.className=class1&classMates[0].name=lily"
parseParam(obj, 'stu');
结果: "stu.name=tom&stu.class.className=class1&stu.classMates[0].name=lily"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//替换url中指定参数
var replaceParam = function(param, value, url, forceReplace) {
url = url || location.href;
var reg = new RegExp("([\\?&]" + param + "=)[^&#]*"), hash = location.hash;
if (!url.match(reg)) {
if (!!hash) {
url = url.substring(0, url.indexOf('#'));
}
return ((url.indexOf("?") == -1) ? (url + "?" + param + "=" + value) : (url + "&" + param +
"=" + value)) + hash;
}
if (forceReplace) {
return url.replace(reg, "$1" + value);
}
return url;
};
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
//文字截断
function truncation() {
if (window.CSS && window.CSS.supports && window.CSS.supports('display', '-webkit-box')) {
return;
}

$('div span').each(function(index, el) {
var line = 0,
step, suffix = "...",
clientRects, isLimit = false,
string = this.innerHTML,
limit = 2; //限制行数

clientRects = this.getClientRects();
line = clientRects.length;

if (line <= limit) {
return;
}

while (line > limit) {
isLimit = true;
step = line > (limit + 1) ? 7 : 1;
string = string.substr(0, string.length - step);
this.innerHTML = string + suffix;
clientRects = this.getClientRects();
line = clientRects.length;
}
if (isLimit) {
string += suffix;
}
$(this).html(string);
});
}

评论