21、原生JavaScript中有insertBefore方法,可惜却没有insertAfter方法?用如下函数实现
function insertAfter(newChild,refChild){ var parElem=refChild.parentNode; if(parElem.lastChild==refChild){ refChild.appendChild(newChild); }else{ parElem.insertBefore(newChild,refChild.nextSibling); } }
22、原生JavaScript中兼容浏览器绑定元素事件
function addEventSamp(obj,evt,fn){ if (obj.addEventListener) { obj.addEventListener(evt, fn, false); }else if(obj.attachEvent){ obj.attachEvent('on'+evt,fn); } }
23、原生JavaScript光标停在文字的后面,文本框获得焦点时调用
function focusLast(){ var e = event.srcElement; var r =e.createTextRange(); r.moveStart('character',e.value.length); r.collapse(true); r.select(); }
24、原生JavaScript检验URL链接是否有效
function getUrlState(URL){ var xmlhttp = new ActiveXObject("microsoft.xmlhttp"); xmlhttp.Open("GET",URL, false); try{ xmlhttp.Send(); }catch(e){ }finally{ var result = xmlhttp.responseText; if(result){ if(xmlhttp.Status==200){ return(true); }else{ return(false); } }else{ return(false); } } }
25、原生JavaScript格式化CSS样式代码
function formatCss(s){//格式化代码 s = s.replace(/\s*([\{\}\:\;\,])\s*/g, "$1"); s = s.replace(/;\s*;/g, ";"); //清除连续分号 s = s.replace(/\,[\s\.\#\d]*{/g, "{"); s = s.replace(/([^\s])\{([^\s])/g, "$1 {\n\t$2"); s = s.replace(/([^\s])\}([^\n]*)/g, "$1\n}\n$2"); s = s.replace(/([^\s]);([^\s\}])/g, "$1;\n\t$2"); return s; }
26、原生JavaScript压缩CSS样式代码
function yasuoCss (s) {//压缩代码 s = s.replace(/\/\*(.|\n)*?\*\//g, ""); //删除注释 s = s.replace(/\s*([\{\}\:\;\,])\s*/g, "$1"); s = s.replace(/\,[\s\.\#\d]*\{/g, "{"); //容错处理 s = s.replace(/;\s*;/g, ";"); //清除连续分号 s = s.match(/^\s*(\S+(\s+\S+)*)\s*$/); //去掉首尾空白 return (s == null) ? "" : s[1]; }
27、原生JavaScript获取当前路径
var currentPageUrl = ""; if (typeof this.href === "undefined") { currentPageUrl = document.location.toString().toLowerCase(); } else { currentPageUrl = this.href.toString().toLowerCase(); }
28、原生JavaScriptIP转成整型
function _ip2int(ip){ var num = 0; ip = ip.split("."); num = Number(ip[0]) * 256 * 256 * 256 + Number(ip[1]) * 256 * 256 + Number(ip[2]) * 256 + Number(ip[3]); num = num >>> 0; return num; }
29、原生JavaScript整型解析为IP地址
function _int2iP(num){ var str; var tt = new Array(); tt[0] = (num >>> 24) >>> 0; tt[1] = ((num << 8) >>> 24) >>> 0; tt[2] = (num << 16) >>> 24; tt[3] = (num << 24) >>> 24; str = String(tt[0]) + "." + String(tt[1]) + "." + String(tt[2]) + "." + String(tt[3]); return str; }
30、原生JavaScript实现checkbox全选与全不选
function checkAll() { var selectall = document.getElementById("selectall"); var allbox = document.getElementsByName("allbox"); if (selectall.checked) { for (var i = 0; i < allbox