最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Firefox outerHTML實現代碼

 更新時間:2009年06月04日 23:56:35   作者:  
firefox沒有outerHTML用以下方法解決

減少DOM數可以加快瀏覽器的在解析頁面過程中DOM Tree和render tree的構建,從而提高頁面性能。為此我們可以把頁面中那些首屏渲染不可見的部分HTML暫存在TextArea中,等完成渲染后再處理這部分HTML來達到這個目的。 要把TextArea 中暫存的HTML內容添加到頁面中,使用元素的outerHTML屬性是最簡單方便的了,不過在DOM標準中并沒有定義outerHTML,支持的瀏覽器有IE6+,safari, operal和 Chrome,經測試FF4.0- 中還不支持。所以我們就來實現一個可以跨瀏覽器的outerHTML。
outerHTML 就是獲取或設置包含元素標簽本身在內的html。下面是實現代碼:

復制代碼 代碼如下:

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指定的屬性個數
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以避免覆蓋瀏覽器原生的實現。
2 "__defineSetter__","__defineGetter__" 是firefox瀏覽器私有方面。分別定義當設置屬性值和獲取屬性要執(zhí)行的操作。
3 在"__defineSetter__" "outerHTML"中為了避免插入頁面中元素過多導致頻繁發(fā)生reflow影響性能。使用了文檔碎片對象fragment來暫存需要插入頁面中DOM元素。
4 在"__defineGetter__" "outerHTML" 中使用元素attributes屬性來遍歷給元素指定的屬性。結合innerHTML返回了包含原屬本身在內的html字符串。
測試代碼:
復制代碼 代碼如下:

<!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指定的屬性個數
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>

假設要獲取 <p id="outerID">sdfdsdfsd</p> 的 P的outerHTML
代碼:
復制代碼 代碼如下:

var _p = document.getElementById('outerID');
_P = _P.cloneNode();
var _DIV = document.createElement();
_DIV.appendChild(_P);
alert(_DIV.innerHTML); 就是P的outerHTML;

firefox沒有outerHTML用以下方法解決
復制代碼 代碼如下:

/**
* 兼容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;
});
}

測試有效.
關于insertAdjacentHTML兼容的解新決辦法
復制代碼 代碼如下:

//---在組件最后插入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);
}
}

關于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>');
復制代碼 代碼如下:

<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>

相關文章

  • js時間查詢插件使用詳解

    js時間查詢插件使用詳解

    這篇文章主要為大家詳細介紹了js時間查詢插件的使用方法,結合bootstrap進行使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • iframe的父子窗口之間的對象相互調用基本用法

    iframe的父子窗口之間的對象相互調用基本用法

    iframe在使用時可能會涉及到父子窗口之間傳值和方法的相互調用,研究了一下其實非常簡單,就那么幾個用法而已,在此與大家分享下,感興趣的朋友可以參考下
    2013-09-09
  • 微信小程序如何使用canvas二維碼保存至手機相冊

    微信小程序如何使用canvas二維碼保存至手機相冊

    這篇文章主要介紹了微信小程序如何使用canvas二維碼保存至手機相冊的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用微信小程序具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-07-07
  • 簡單的JS控制button顏色隨點擊更改的實現方法

    簡單的JS控制button顏色隨點擊更改的實現方法

    下面小編就為大家?guī)硪黄唵蔚腏S控制button顏色隨點擊更改的實現方法。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • 利用微信小程序制作屬于自己的Icon圖標

    利用微信小程序制作屬于自己的Icon圖標

    項目中常常需要使用到字體圖標,微信小程序中使用字體圖標與在平常的web前端中類似但是又有區(qū)別,下面這篇文章主要給大家介紹了關于利用微信小程序制作屬于自己的Icon圖標的相關資料,需要的朋友可以參考下
    2022-04-04
  • MUI頂部選項卡的用法(tab-top-webview-main)詳解

    MUI頂部選項卡的用法(tab-top-webview-main)詳解

    最近用MUI做手機app應用的時候,遇到的小bug,這里小編給大家分享MUI頂部選項卡的用法(tab-top-webview-main)詳解,感興趣的朋友一起看看吧
    2017-10-10
  • JS正則表達式之非捕獲分組用法實例分析

    JS正則表達式之非捕獲分組用法實例分析

    這篇文章主要介紹了JS正則表達式之非捕獲分組用法,結合實例形式詳細分析了正則表達式中非捕獲分組的概念、功能、使用方法與相關注意事項,需要的朋友可以參考下
    2016-12-12
  • 非阻塞動態(tài)加載javascript廣告實現代碼

    非阻塞動態(tài)加載javascript廣告實現代碼

    非阻塞動態(tài)加載javascript廣告,需要的朋友可以參考下。
    2010-11-11
  • JavaScript實現的微信二維碼圖片生成器的示例

    JavaScript實現的微信二維碼圖片生成器的示例

    二維碼分享功能大多是由后端實現的,對服務器的負載較重,這里有一個前端實現的版本,本文介紹了JavaScript實現的微信二維碼圖片生成器的示例,有需要的可以了解一下。
    2016-10-10
  • TypeScript聲明合并的實現

    TypeScript聲明合并的實現

    本文主要介紹了TypeScript聲明合并的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07

最新評論

汉源县| 克什克腾旗| 河间市| 新昌县| 衡南县| 花垣县| 普宁市| 惠东县| 奉新县| 江都市| 广平县| 海城市| 怀安县| 临洮县| 临沧市| 纳雍县| 南阳市| 舒兰市| 基隆市| 肇庆市| 枞阳县| 延寿县| 尼木县| 绵阳市| 通化市| 静宁县| 汉寿县| 阿城市| 沧源| 洪湖市| 蛟河市| 墨玉县| 婺源县| 桃园县| 清流县| 乌鲁木齐市| 玉门市| 长兴县| 田林县| 屏边| 温州市|