天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

Firefox outerHTML實(shí)現(xiàn)代碼

減少DOM數(shù)可以加快瀏覽器的在解析頁面過程中DOM Tree和render tree的構(gòu)建,從而提高頁面性能。為此我們可以把頁面中那些首屏渲染不可見的部分HTML暫存在TextArea中,等完成渲染后再處理這部分HTML來達(dá)到這個(gè)目的。 要把TextArea 中暫存的HTML內(nèi)容添加到頁面中,使用元素的outerHTML屬性是最簡單方便的了,不過在DOM標(biāo)準(zhǔn)中并沒有定義outerHTML,支持的瀏覽器有IE6+,safari, operal和 Chrome,經(jīng)測試FF4.0- 中還不支持。所以我們就來實(shí)現(xiàn)一個(gè)可以跨瀏覽器的outerHTML。
outerHTML 就是獲取或設(shè)置包含元素標(biāo)簽本身在內(nèi)的html。下面是實(shí)現(xiàn)代碼:
復(fù)制代碼 代碼如下:
if(typeof HTMLElement !== "undefined" && !("outerHTML" in HTMLElement.prototype)) {
//console.log("defined outerHTML");
HTMLElement.prototype.__defineSetter__("outerHTML",function(str){
var fragment = document.createDocumentFragment();
var div = document.createElement("div");
div.innerHTML = str;
for(var i=0, n = div.childNodes.length; i<n; i++){
fragment.appendChild(div.childNodes[i]);
}
this.parentNode.replaceChild(fragment, this);
});
//
HTMLElement.prototype.__defineGetter__("outerHTML",function(){
var tag = this.tagName;
var attributes = this.attributes;
var attr = [];
//for(var name in attributes){//遍歷原型鏈上成員
for(var i=0,n = attributes.length; i<n; i++){//n指定的屬性個(gè)數(shù)
if(attributes[i].specified){
attr.push(attributes[i].name + '="' + attributes[i].value + '"');
}
}
return ((!!this.innerHTML) ?
'<' + tag + ' ' + attr.join(' ')+'>'+this.innerHTML+'</'+tag+'>' :
'<' + tag + ' ' +attr.join(' ')+'/>');
});
}

代碼說明:
1 代碼中首先條件判斷來監(jiān)測瀏覽器是否支持outerHTML以避免覆蓋瀏覽器原生的實(shí)現(xiàn)。
2 "__defineSetter__","__defineGetter__" 是firefox瀏覽器私有方面。分別定義當(dāng)設(shè)置屬性值和獲取屬性要執(zhí)行的操作。
3 在"__defineSetter__" "outerHTML"中為了避免插入頁面中元素過多導(dǎo)致頻繁發(fā)生reflow影響性能。使用了文檔碎片對象fragment來暫存需要插入頁面中DOM元素。
4 在"__defineGetter__" "outerHTML" 中使用元素attributes屬性來遍歷給元素指定的屬性。結(jié)合innerHTML返回了包含原屬本身在內(nèi)的html字符串。
測試代碼:
復(fù)制代碼 代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>outerHTML</title>
</head>
<body>
<div id="content" class="test">
<p>This is <strong>paragraph</strong> with a list following it</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>
</div>
<script>
if(typeof HTMLElement !== "undefined" && !("outerHTML" in HTMLElement.prototype)) {
console.log("defined outerHTML");
HTMLElement.prototype.__defineSetter__("outerHTML",function(str){
var fragment = document.createDocumentFragment();
var div = document.createElement("div");
div.innerHTML = str;
for(var i=0, n = div.childNodes.length; i<n; i++){
fragment.appendChild(div.childNodes[i]);
}
this.parentNode.replaceChild(fragment, this);
});
//
HTMLElement.prototype.__defineGetter__("outerHTML",function(){
var tag = this.tagName;
var attributes = this.attributes;
var attr = [];
//for(var name in attributes){//遍歷原型鏈上成員
for(var i=0,n = attributes.length; i<n; i++){//n指定的屬性個(gè)數(shù)
if(attributes[i].specified){
attr.push(attributes[i].name + '="' + attributes[i].value + '"');
}
}
return ((!!this.innerHTML) ?
'<' + tag + ' ' + attr.join(' ')+'>'+this.innerHTML+'</'+tag+'>' :
'<' + tag + ' ' +attr.join(' ')+'/>');
});
}
var content = document.getElementById("content");
alert(content.outerHTML)
</script>
</body>
</html>

假設(shè)要獲取 <p id="outerID">sdfdsdfsd</p> 的 P的outerHTML
代碼:
復(fù)制代碼 代碼如下:
var _p = document.getElementById('outerID');
_P = _P.cloneNode();
var _DIV = document.createElement();
_DIV.appendChild(_P);
alert(_DIV.innerHTML); 就是P的outerHTML;

firefox沒有outerHTML用以下方法解決
復(fù)制代碼 代碼如下:
/**
* 兼容firefox的 outerHTML 使用以下代碼后,firefox可以使用element.outerHTML
**/
if(window.HTMLElement) {
HTMLElement.prototype.__defineSetter__("outerHTML",function(sHTML){
var r=this.ownerDocument.createRange();
r.setStartBefore(this);
var df=r.createContextualFragment(sHTML);
this.parentNode.replaceChild(df,this);
return sHTML;
});
HTMLElement.prototype.__defineGetter__("outerHTML",function(){
var attr;
var attrs=this.attributes;
var str="<"+this.tagName.toLowerCase();
for(var i=0;i<attrs.length;i++){
attr=attrs[i];
if(attr.specified)
str+=" "+attr.name+'="'+attr.value+'"';
}
if(!this.canHaveChildren)
return str+">";
return str+">"+this.innerHTML+"</"+this.tagName.toLowerCase()+">";
});
HTMLElement.prototype.__defineGetter__("canHaveChildren",function(){
switch(this.tagName.toLowerCase()){
case "area":
case "base":
case "basefont":
case "col":
case "frame":
case "hr":
case "img":
case "br":
case "input":
case "isindex":
case "link":
case "meta":
case "param":
return false;
}
return true;
});
}

測試有效.
關(guān)于insertAdjacentHTML兼容的解新決辦法
復(fù)制代碼 代碼如下:
//---在組件最后插入html代碼
function InsertHtm(op,code,isStart){
if(Dvbbs_IsIE5)
op.insertAdjacentHTML(isStart ? "afterbegin" : "afterEnd",code);
else{
var range=op.ownerDocument.createRange();
range.setStartBefore(op);
var fragment = range.createContextualFragment(code);
if(isStart)
op.insertBefore(fragment,op.firstChild);
else
op.appendChild(fragment);
}
}

關(guān)于inner/outerHTML在NC6中的參考
DOM level 1 has no methods to allow for insertion of unparsed HTML into the document tree (as IE allows with insertAdjacentHTML or assignment to inner/outerHTML).NN6 (currently in beta as NN6PR3) know supports the .innerHTMLproperty of HTMLElements so that you can read or write the innerHTML of a page element like in IE4+.NN6 also provides a DOM level 2 compliant Range object to which a createContextualFragment('html source string')was added to spare DOM scripters the task of parsing html and creating DOM elements.You create a Range with var range = document.createRange();Then you should set its start point to the element where you want to insert the html for instance var someElement = document.getElementById('elementID'); range.setStartAfter(someElement);Then you create a document fragment from the html source to insert for example var docFrag = range.createContextualFragment('<P>Kibology for all.</P>');and insert it with DOM methods someElement.appendChild(docFrag);The NETscape JavaScript 1.5 version even provides so called setters for properties which together with the ability to prototype the DOM elements allows to emulate setting of outerHMTL for NN6:<SCRIPT LANGUAGE="JavaScript1.5">if (navigator.appName == 'NETscape') { HTMLElement.prototype.outerHTML setter = function (html) { this.outerHTMLInput = html; var range = this.ownerDocument.createRange(); range.setStartBefore(this); var docFrag = range.createContextualFragment(html); this.parentNode.replaceChild(docFrag, this); }}</SCRIPT> If you insert that script block you can then write cross browser code assigning to .innerHTML .outerHTMLfor instance document.body.innerHTML = '<P>Scriptology for all</P>';which works with both IE4/5 and NN6.The following provides getter functions for .outerHTMLto allow to read those properties in NN6 in a IE4/5 compatible way. Note that while the scheme of traversing the document tree should point you in the right direction the code example might not satisfy your needs as there are subtle difficulties when trying to reproduce the html source from the document tree. See for yourself whether you like the result and improve it as needed to cover other exceptions than those handled (for the empty elements and the textarea element).<HTML><HEAD><STYLE></STYLE><SCRIPT LANGUAGE="JavaScript1.5">var emptyElements = { HR: true, BR: true, IMG: true, INPUT: true};var specialElements = { TEXTAREA: true};HTMLElement.prototype.outerHTML getter = function () { return getOuterHTML (this);}function getOuterHTML (node) { var html = ''; switch (node.nodeType) { case Node.ELEMENT_NODE: html += '<'; html += node.nodeName; if (!specialElements[node.nodeName]) { for (var a = 0; a < node.attributes.length; a++) html += ' ' + node.attributes[a].nodeName.toUpperCase() + '="' + node.attributes[a].nodeValue + '"'; html += '>'; if (!emptyElements[node.nodeName]) { html += node.innerHTML; html += '<//' + node.nodeName + '>'; } } else switch (node.nodeName) { case 'TEXTAREA': for (var a = 0; a < node.attributes.length; a++) if (node.attributes[a].nodeName.toLowerCase() != 'value') html += ' ' + node.attributes[a].nodeName.toUpperCase() + '="' + node.attributes[a].nodeValue + '"'; else var content = node.attributes[a].nodeValue; html += '>'; html += content; html += '<//' + node.nodeName + '>'; break; } break; case Node.TEXT_NODE: html += node.nodeValue; break; case Node.COMMENT_NODE: html += '<!' + '--' + node.nodeValue + '--' + '>'; break; } return html;}</SCRIPT></HEAD><BODY><A HREF="Javascript: alert(document.documentElement.outerHTML); void 0">show document.documentElement.outerHTML</A>|<A HREF="Javascript: alert(document.body.outerHTML); void 0">show document.body.outerHTML</A>|<A HREF="Javascript: alert(document.documentElement.innerHTML); void 0">show document.documentElement.innerHTML</A>|<A HREF="Javascript: alert(document.body.innerHTML); void 0">show document.body.innerHTML</A><FORM NAME="formName"><TEXTAREA NAME="aTextArea" ROWS="5" COLS="20">JavaScript.FAQTs.comKibology for all.</TEXTAREA></FORM><DIV><P>JavaScript.FAQTs.com</P><BLOCKQUOTE>Kibology for all.<BR>All for Kibology.</BLOCKQUOTE></DIV></BODY></HTML>Note that the getter/setter feature is experimental and its syntax is subject to change.
HTMLElement.prototype.innerHTML setter = function (str) { var r = this.ownerDocument.createRange(); r.selectNodeContents(this); r.deleteContents(); var df = r.createContextualFragment(str); this.appendChild(df); return str;}HTMLElement.prototype.outerHTML setter = function (str) { var r = this.ownerDocument.createRange(); r.setStartBefore(this); var df = r.createContextualFragment(str); this.parentNode.replaceChild(df, this); return str;}
HTMLElement.prototype.innerHTML getter = function () { return getInnerHTML(this);}
function getInnerHTML(node) { var str = ""; for (var i=0; i<node.childNodes.length; i++) str += getOuterHTML(node.childNodes.item(i)); return str;}
HTMLElement.prototype.outerHTML getter = function () { return getOuterHTML(this)}
function getOuterHTML(node) { var str = ""; switch (node.nodeType) { case 1: // ELEMENT_NODE str += "<" + node.nodeName; for (var i=0; i<node.attributes.length; i++) { if (node.attributes.item(i).nodeValue != null) { str += " " str += node.attributes.item(i).nodeName; str += "=/""; str += node.attributes.item(i).nodeValue; str += "/""; } }
if (node.childNodes.length == 0 && leafElems[node.nodeName]) str += ">"; else { str += ">"; str += getInnerHTML(node); str += "<" + node.nodeName + ">" } break; case 3: //TEXT_NODE str += node.nodeValue; break; case 4: // CDATA_SECTION_NODE str += "<![CDATA[" + node.nodeValue + "]]>"; break; case 5: // ENTITY_REFERENCE_NODE str += "&" + node.nodeName + ";" break;
case 8: // COMMENT_NODE str += "<!--" + node.nodeValue + "-->" break; }
return str;}
var _leafElems = ["IMG", "HR", "BR", "INPUT"];var leafElems = {};for (var i=0; i<_leafElems.length; i++) leafElems[_leafElems[i]] = true;
然后我們可以封成JS引用
if (/Mozilla//5/.0/.test(navigator.userAgent)) document.write('<script type="text/Javascript" src="mozInnerHTML.js"></sc' + 'ript>');
復(fù)制代碼 代碼如下:
<script language="JavaScript" type="Text/JavaScript">
<!--
var emptyElements = { HR: true, BR: true, IMG: true, INPUT: true }; var specialElements = { TEXTAREA: true };
HTMLElement.prototype.outerHTML getter = function() {
return getOuterHTML(this);
}
function getOuterHTML(node) {
var html = '';
switch (node.nodeType) {
case Node.ELEMENT_NODE: html += '<'; html += node.nodeName; if (!specialElements[node.nodeName]) {
for (var a = 0; a < node.attributes.length; a++)
html += ' ' + node.attributes[a].nodeName.toUpperCase() + '="' + node.attributes[a].nodeValue + '"';
html += '>';
if (!emptyElements[node.nodeName]) {
html += node.innerHTML;
html += '<//' + node.nodeName + '>';
}
} else
switch (node.nodeName) {
case 'TEXTAREA': for (var a = 0; a < node.attributes.length; a++)
if (node.attributes[a].nodeName.toLowerCase() != 'value')
html
+= ' ' + node.attributes[a].nodeName.toUpperCase() + '="' + node.attributes[a].nodeValue
+ '"';
else
var content = node.attributes[a].nodeValue;
html += '>'; html += content; html += '<//' + node.nodeName + '>'; break;
} break;
case Node.TEXT_NODE: html += node.nodeValue; break;
case Node.COMMENT_NODE: html += '<!' + '--' + node.nodeValue + '--' + '>'; break;
}
return html;
}
//-->
</script>

JavaScript技術(shù)Firefox outerHTML實(shí)現(xiàn)代碼,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: chinese学生gv video| 男人边吃奶边挵进去呻吟漫画 | 国产精品人妻99一区二 | 亚洲 欧美 国产在线视频 | 亚洲免费视频在线 | yellow免费影视大全 | 鸡鸡插屁股 | 啪啪漫画无遮挡全彩h同人 啪啪激情婷婷久久婷婷色五月 | 免费又黄又硬又爽大片 | 成品片a免人看免费 | 秋霞电影在线观看午夜伦 | 金发欧美一区在线观看 | 国产成人啪精视频精东传媒网站 | G国产精品无马 | 十七岁日本免费完整版BD | 欧美精品成人一区二区在线观看 | 999精品在线 | 日韩一区二区三区视频在线观看 | 国产69精品久久久久人妻刘玥 | 午夜精品久久久久久久99蜜桃 | 真人女人无遮挡内谢免费视频% | 久久综合久久鬼 | 日韩精品久久久久久久电影 | 999久久久无码国产精蜜柚 | 俄罗斯12一15处交 | 三级色视频 | 丰满女朋友在线观看中文 | 精精国产www视频在线观看免费 | 午夜福利理论片在线播放 | 野花影院手机在线观看 | 果冻传媒在线观看资源七夕 | 5g在视影讯天天5g免费观看 | 日本无码色哟哟婷婷最新网站 | 伊人久久大香线蕉综合影 | 白丝高中生被c爽哭 | 5g天天影院天天看天天爽 | 儿媳妇完整版视频播放免费观看 | 精品国产午夜福利在线观看蜜月 | 大香网伊人久久综合观看 | 99视频精品全部免费观看 | 伦理片天堂eeuss影院2o12 |