请各位读者添加一下作者的微信公众号,以后有新的文章,将在微信公众号直接推送给各位,非常感谢。
0.前言
说什么好呢?
完全不知道呀。
要不就这么地吧!
1.原生JavaScript实现字符串长度截取
function cutstr(str, len) {
var temp;
var icount = 0;
var patrn = /[^\x00-\xff]/;
var strre = "";
for (var i = 0; i < str.length; i++) {
if (icount < len - 1) {
temp = str.substr(i, 1);
if (patrn.exec(temp) == null) {
icount = icount + 1
} else {
icount = icount + 2
}
strre += temp
} else {
break
}
}
return strre + "..."
}
2.原生JavaScript获取域名主机
function getHost(url) {
var host = "null";
if(typeof url == "undefined"|| null == url) {
url = window.location.href;
}
var regex = /^\w+\:\/\/([^\/]*).*/;
var match = url.match(regex);
if(typeof match != "undefined" && null != match) {
host = match[1];
}
return host;
}
3.原生JavaScript元素显示的通用方法
function $(id) {
return !id ? null : document.getElementById(id);
}
function display(id) {
var obj = $(id);
if(obj.style.visibility) {
obj.style.visibility = obj.style.visibility == 'visible' ? 'hidden' : 'visible';
} else {
obj.style.display = obj.style.display == '' ? 'none' : '';
}
}
4.原生JavaScript实现checkbox全选与全不选
function checkAll() {
var selectall = document.getElementById("selectall");
var allbox = document.getElementsByName("allbox");
if (selectall.checked) {
for (var i = 0; i < allbox.length; i++) {
allbox[i].checked = true;
}
} else {
for (var i = 0; i < allbox.length; i++) {
allbox[i].checked = false;
}
}
}
5.原生JavaScript完美判断是否为网址
function IsURL(strUrl) {
var regular = /^\b(((https?|ftp):\/\/)?[-a-z0-9]+(\.[-a-z0-9]+)*\.(?:com|edu|gov|int|mil|net|org|biz|info|name|museum|asia|coop|aero|[a-z][a-z]|((25[0-5])|(2[0-4]\d)|(1\d\d)|([1-9]\d)|\d))\b(\/[-a-z0-9_:\@&?=+,.!\/~%\$]*)?)$/i
if (regular.test(strUrl)) {
return true;
}
else {
return false;
}
}
6.原生JavaScript获得URL中GET参数值
// 用法:如果地址是 test.htm?t1=1&t2=2&t3=3, 那么能取得:GET["t1"], GET["t2"], GET["t3"]
function get_get(){
querystr = window.location.href.split("?")
if(querystr[1]){
GETs = querystr[1].split("&")
GET =new Array()
for(i=0;i
7.原生JavaScript跨浏览器添加事件
function addEvt(oTarget,sEvtType,fnHandle){
if(!oTarget){return;}
if(oTarget.addEventListener){
oTarget.addEventListener(sEvtType,fnHandle,false);
}else if(oTarget.attachEvent){
oTarget.attachEvent("on" + sEvtType,fnHandle);
}else{
oTarget["on" + sEvtType] = fnHandle;
}
}
8.原生JavaScript实现返回顶部的通用方法
function backTop(btnId) {
var btn = document.getElementById(btnId);
var d = document.documentElement;
var b = document.body;
window.onscroll = set;
btn.style.display = "none";
btn.onclick = function() {
btn.style.display = "none";
window.onscroll = null;
this.timer = setInterval(function() {
d.scrollTop -= Math.ceil((d.scrollTop + b.scrollTop) * 0.1);
b.scrollTop -= Math.ceil((d.scrollTop + b.scrollTop) * 0.1);
if ((d.scrollTop + b.scrollTop) == 0) clearInterval(btn.timer, window.onscroll = set);
},
10);
};
function set() {
btn.style.display = (d.scrollTop + b.scrollTop > 100) ? 'block': "none"
}
};
backTop('goTop');
9.原生JavaScript实现全选通用方法
function checkall(form, prefix, checkall) {
var checkall = checkall ? checkall : 'chkall';
for(var i = 0; i < form.elements.length; i++) {
var e = form.elements[i];
if(e.type=="checkbox"){
e.checked = form.elements[checkall].checked;
}
}
}
10.原生JavaScript实现全部取消选择通用方法
function uncheckAll(form) {
for (var i=0;i
11.原生JavaScript获取单选按钮的值
function get_radio_value(field){
if(field&&field.length){
for(var i=0;i
12.原生JavaScript获取复选框的值
function get_checkbox_value(field){
if(field&&field.length){
for(var i=0;i
13.原生JavaScript判断变量是否空值
/**
* 判断变量是否空值
* undefined, null, '', false, 0, [], {} 均返回true,否则返回false
*/
function empty(v){
switch (typeof v){
case 'undefined' : return true;
case 'string' : if(trim(v).length == 0) return true; break;
case 'boolean' : if(!v) return true; break;
case 'number' : if(0 === v) return true; break;
case 'object' :
if(null === v) return true;
if(undefined !== v.length && v.length==0) return true;
for(var k in v){return false;} return true;
break;
}
return false;
}
14.后记
本文参考于 小萧ovo 的码农在线,感谢作者。