Firefox outerHTML实现代码_javascript技巧_脚本之家

  • Item 1
  • Item 2
  • Item 3
  • Item 4


Kibology for all.
All for Kibology.

JavaScript.FAQTs.comKibology for all.

If you insert that script block you can then write cross browser code
assigning to .innerHTML .outerHTMLfor instance document.body.innerHTML =

‘);and insert it with DOM methods someElement.appendChild;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:

This is paragraph with a list following it

的 P的outerHTML 代码: 复制代码 代码如下:
var _p = document.getElementById; _P = _P.cloneNode(); var _DIV =
document.createElement(); _DIV.appendChild; alert; 正是P的outerHTML;
firefox未有outerHTML用以下办法消除 复制代码 代码如下: /** * 宽容firefox的
outerHTML 使用以下代码后,firefox能够动用element.outerHTML **/ if {
HTMLElement.prototype.__defineSetter__(“outerHTML”,function{ var
r=this.ownerDocument.createRange(); r.setStartBefore; var
df=r.createContextualFragment; this.parentNode.replaceChild; return
sHTML; });
HTMLElement.prototype.__defineGetter__(“outerHTML”,function(){ var
attr; var attrs=this.attributes; var str=””; return
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{ if op.insertAdjacentHTML(isStart ? “afterbegin” :
“afterEnd”,codeState of Qatar; else{ var range=op.ownerDocument.createRange(卡塔尔国;
range.setStartBefore; var fragment = range.createContextualFragment
op.insertBefore(fragment,op.firstChildState of Qatar; else op.appendChild; } }
关于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 NN6P大切诺基3卡塔尔(قطر‎ 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
createContextualFragmentwas 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; range.setStartAfter;Then you create a document
fragment from the html source to insert for example var docFrag =

‘;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).



Kibology for all.

Note that the getter/setter feature is experimental and its syntax is
subject to change. HTMLElement.prototype.innerHTML setter = function {
var r = this.ownerDocument.createRange(); r.selectNodeContents;
r.deleteContents(); var df = r.createContextualFragment;
this.appendChild; return str;}HTMLElement.prototype.outerHTML setter =
function { var r = this.ownerDocument.createRange(); r.setStartBefore;
var df = r.createContextualFragment; this.parentNode.replaceChild;
return str;} HTMLElement.prototype.innerHTML getter = function () {
return getInnerHTML;} function getInnerHTML { var str = “”; for (var
i=0; i”; else { str += “>”; str += getInnerHTML; str += “” } break;
case 3: //TEXT_NODE str += node.nodeValue; break; case 4: //
CDATA_SECTION_NODE str += “”; break; case 5: //
ENTITY_REFERENCE_NODE str += “&” + node.nodeName + “;” break; case 8:
// COMMENT_NODE str += “” break; } return str;} var _leafElems =
[“IMG”, “HR”, “BR”, “INPUT”];var leafElems = {};for (var i=0;
i<_leafelems.length i=”” leafelems=”” true=”” if=””
document.write=”” type=”text/javascript” src=”mozInnerHTML.js”>’State of Qatar;
复制代码 代码如下:

压缩DOM数可以加快浏览器的在拆解深入分析页面进度中DOM Tree和render
operal和 Chrome,经测量检验FF4.0-
中还不协理。所以大家就来实现一个能够跨浏览器的outerHTML。 outerHTML
正是赢得或设置包涵成分标签本人在内的html。上边是贯彻代码: 复制代码 代码如下: if(typeof HTMLElement !==
“undefined” && !(“outerHTML” in HTMLElement.prototype)) { //console.log;
HTMLElement.prototype.__defineSetter__(“outerHTML”,function{ var
fragment = document.createDocumentFragment(State of Qatar; var div =
document.createElement; div.innerHTML = str; for(var i=0, n =
div.childNodes.length; i’+this.innerHTML+”+tag+’>’ : ‘复制代码

Scriptology for all




电子邮件地址不会被公开。 必填项已用*标注