Util = new UtilTools(); /** * 本代码可随意使用,请保留下面的网址. * WebSite: */ function UtilTools() { //取元素
this.gE = function (el) { if(typeof el == 'string') return document.getElementById(el); else return el; } //取嵌套页面的元素
this.gFE = function (win,el) { try{ if(typeof el == 'string') return document.getElementById(win).contentWindow.document.getElementById(el); else return document.getElementById(win).contentWindow.document.el; }catch(e){throw e;} } //判断浏览器
this.isIE = function () { //Detect:BrowserDetector.js
//google.com
return Detect.IE(); } //取页面被选中部分
this.gR = function (el) { try{ if(typeof el == 'undefined' && !isIE()) return document.getSelection(); if(this.isIE()) if(window.getSelection) return window.getSelection(); else if(document.getSelection) return document.getSelection(); else if(document.selection) return document.selection.createRange().text; else return; else el = this.gE(el); return el.value.substr(el.selectionStart,el.selectionEnd - el.selectionStart); }catch(e){throw e;} } //取嵌套页面被选中部分
this.gFR = function (frame,el) { try{ if(this.isIE()) { var win = this.gE(frame).contentWindow; var doc = win.document; if(win.getSelection) return win.getSelection(); else if(doc.getSelection) return doc.getSelection(); else if(doc.selection) return doc.selection.createRange().htmlText; else return; }else{ if(typeof el == 'undefined') return this.gE(frame).contentWindow.document.getSelection(); else el = this.gFE(frame,el); return el.value.substr(el.selectionStart,el.selectionEnd - el.selectionStart); } }catch(e){throw e;} } //取嵌套页面的 document 内容
this.gFC = function (frame) { return this.gFD(frame).body.innerHTML; } //取嵌套页面的 document 对象
this.gFD = function (frame) { return this.gE(frame).contentWindow.document; } //取嵌套页面的 window 对象
this.gFW = function (frame) { return this.gE(frame).contentWindow; } //替换嵌套页面选中部分
this.sFR = function (win,text) { try{ if(this.isIE()) { var rang = this.gFD(win).selection.createRange(); text = this.rH(text); rang.pasteHTML(text); return range; }else{ var range = this.gFW(win).getSelection().getRangeAt(0); range.deleteContents(); var df = document.createDocumentFragment(); var span = document.createElement('div'); span.innerHTML = text; df.appendChild(span.firstChild); range.insertNode(df); return range; } }catch(e){throw e;} } this.setChild = function (pnode,cnode) { pnode.appendChild(cnode); } //追加元素到页面
/** * nodeName: 要追加的标签类型 形式为 tagname:type:name * strId: 要追加的标签的ID * appendTo:追加到指定元素里.默认为document * label:针对该标签的label */ this.aE = function (nodeName, strId, appendTo,label) { var node = nodeName.split(':'); if(this.isIE() && node[2]){//fix ie
var el = document.createElement('<' + node[0] + ' name="' + node[2] + '"/>'); }else{ var el = document.createElement(node[0]); } if(node[1]) el.setAttribute('type',node[1]); if(node[2]) el.setAttribute('name',node[2]); if(strId) el.setAttribute("id", strId); if (appendTo) { this.gE(appendTo).appendChild(el); } else { document.body.appendChild(el); } if(label) { if(node[1] && node[1] == 'button') { el.value = label; }else{ var lab = document.createElement('label'); lab.setAttribute('for',strId); lab.innerHTML = label + ':'; el.parentNode.insertBefore(lab,el); } } return el; }; //替换当前页面选中元素内容
this.sR = function (el,text) { try{ if(this.isIE()) { var s = this.gR(el); var exp = new RegExp(s); this.gE(el).value = this.gE(el).value.replace(exp,text); return this.gE(el); }else{ var s = this.gR(el); var exp = new RegExp(s); this.gE(el).value = this.gE(el).value.replace(exp,text); return this.gE(el); } }catch(e){throw e;} } //用指定 标签 环绕选中区域
this.aT = function (el,stag,etag) { try{ var r = this.gR(el); var s = stag + r + etag; this.sR(el,s); }catch(e){throw e;} } //追加内容到选中区域前
this.aFRB = function (win,text) { try{ if(this.isIE()) { var rang = this.gFD(win).selection.createRange(); var r = this.gFR(win); text = this.rH(text); rang.pasteHTML(text + r); return range; }else{ var range = this.gFW(win).getSelection().getRangeAt(0); var df = document.createDocumentFragment(); var span = document.createElement('div'); span.innerHTML = text; df.appendChild(span.firstChild); range.insertNode(df); span = null; df = null; return range; } }catch(e){throw e;} } //代码补全,只补闭标签,IE 下补全自定义标签需要 xmlns
//
// ,
this.rH = function (text) { var d = document.createElement('div'); d.innerHTML = text; return d.innerHTML; } }
|