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

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

如若要拿到

Kibology for all.
All for Kibology.

JavaScript.FAQTs.com

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
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{ 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 =
range.createContextualFragment(‘

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

outerHTML

show
document.documentElement.outerHTML;%20void%200)|show
document.body.outerHTML;%20void%200)|show
document.documentElement.innerHTML;%20void%200)|show
document.body.innerHTML;%20void%200)

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

sdfdsdfsd

发表评论

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

网站地图xml地图