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

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

if(attributes[i].specified){

attr.push(attributes[i].name + '="' + attributes[i].value + '"');

}

}

return ((!!this.innerHTML) ?

''+this.innerHTML+''+tag+'>' :

'');

});

}

代码说明:

1 代码中首先条件判断来监测浏览器是否支持outerHTML以避免覆盖浏览器原生的实现。

2 "__defineSetter__","__defineGetter__" 是firefox浏览器私有方面。分别定义当设置属性值和获取属性要执行的操作。

3 在"__defineSetter__" "outerHTML"中为了避免插入页面中元素过多导致频繁发生reflow影响性能。使用了文档碎片对象fragment来暂存需要插入页面中DOM元素。

4 在"__defineGetter__" "outerHTML" 中使用元素attributes属性来遍历给元素指定的属性。结合innerHTML返回了包含原属本身在内的html字符串。

测试代码:

outerHTML

This is paragraph with a list following it

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

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

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

if(attributes[i].specified){

attr.push(attributes[i].name + '="' + attributes[i].value + '"');

}

}

return ((!!this.innerHTML) ?

''+this.innerHTML+''+tag+'>' :

'');

});

}

var content = document.getElementById("content");

alert(content.outerHTML)

假设要获取

sdfdsdfsd

的 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="

for(var i=0;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('

Kibology for all.

');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: If you insert that script block you can then write cross browser code assigning to .innerHTML .outerHTMLfor instance document.body.innerHTML = '

Scriptology for all

';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).show document.documentElement.outerHTML|show document.body.outerHTML|show document.documentElement.innerHTML|show document.body.innerHTMLJavaScript.FAQTs.comKibology for all.

JavaScript.FAQTs.com

Kibology for all.
All for Kibology.

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

HTMLElement.prototype.outerHTML getter = function () { return getOuterHTML(this)}

function getOuterHTML(node) { var str = ""; switch (node.nodeType) { case 1: // ELEMENT_NODE str += "

if (node.childNodes.length == 0 && leafElems[node.nodeName]) str += ">"; else { str += ">"; str += getInnerHTML(node); 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>

然后我们可以封成JS引用

if (/Mozilla\/5\.0/.test(navigator.userAgent)) document.write('

火狐控制台的html,Firefox outerHTML实现代码相关推荐

  1. 如何利用火狐控制台下载网页图片

    今天在群里看到一个群友的问题,怎么下载网页的图片,经过一番的演变,得出了本文的成果,也算是一种思路,还可以演变成干很多事,因此写下此文,希望能够对大家有所启发. 问题: 如何从一个网页里下载浏览器加载 ...

  2. 火狐浏览器中国版(Firefox) v31.0 beta9 官网正式版

    火狐浏览器中国版(Firefox) v31.0 beta9 官网正式版 软件大小:28.3MB 软件语言:简体中文 软件性质:常用软件 软件授权:官方版 更新时间:2014-07-13 应用平台:Wi ...

  3. c语言控制台光标的坐标范围,C语言之实现控制台光标随意移动的实例代码

    原理引入windows.h,首先是要获得输入的东西,然后通过判断: 1.方向键:执行上下左右的移动功能 2 .回车键:执行换行的功能. 3.普通键:输入功能. 终点就是要获取到屏幕上的坐标,当按下了方 ...

  4. 用坐标实现物体的移动c语言,C语言之实现控制台光标随意移动的实例代码

    原理引入windows.h,首先是要获得输入的东西,然后通过判断: 1.方向键:执行上下左右的移动功能 2 .回车键:执行换行的功能. 3.普通键:输入功能. 终点就是要获取到屏幕上的坐标,当按下了方 ...

  5. VC++6.0 win32 控制台应用程序 简单应用 附代码

    **VC++6.0 win32 控制台应用程序 简单应用 附代码 ** .cpp 文件名 注意:在源文件添加如下声明: #include //没有.h using namespace std;//使用 ...

  6. csgo服务器显示指令,CSGO国服控制台怎么打开 CSGO国服指令代码大全

    CSGO国服的控制台怎么打开?其中相关的设置都有哪些指令?很多玩家们在CSGO国服中不知道控制台的打开方式和相关的指令,为了让玩家们都了解这些,今天一游网小编小熊巴巴就为各位玩家们带来了CSGO国服控 ...

  7. php 兼容火狐,HTML_总结CSS中火狐浏览器与IE浏览器的兼容代码,如何让你写的代码更兼容火狐 - phpStudy...

    总结CSS中火狐浏览器与IE浏览器的兼容代码 如何让你写的代码更兼容火狐和IE两大主流浏览器?本文将总结总结CSS中火狐浏览器与IE浏览器的兼容代码,兼容你兼容主要是语法规范问题,你写CSS写规范了, ...

  8. 火狐量子浏览器怎么样,firefox quantum下载

    火狐量子浏览器中文版是一款先进.优秀且免费开源的老牌网络浏览器,是很多高手们极度热爱的产品.近年来版本更新非常迅速,在速度性 能.安全 性.兼容 性方面均表现优异!在经历了 50 多个版本的迭代更新后 ...

  9. 【Firefox】火狐浏览器网页翻译,只需代码加入书签即可

    Firefox火狐浏览器网页翻译,只需将下面的代码加入到书签,用时点一下这个书签即可,勿需安装扩展插件. 建议放在书签工具栏中,这样用起来更方便.三个代码都不错哦! 一.有道词典适合整页+局部 jav ...

最新文章

  1. A monad tutorial for Clojure programmers (part 3)
  2. 代码重构 防火墙 相关知识
  3. 漂亮的个人团队介绍网页模板
  4. CSS中可以和不可以继承的属性
  5. oracle数据库看开销,【Database】AIX系统下跟踪开销大的Oracle数据库进程
  6. XMPP聊天环境配置
  7. 公司拿到了量化交易模型, 交易员和策略师就可滚蛋了?
  8. ads s参数拟合_在ADS仿真或者查看S参数的方法
  9. BLE蓝牙4.0串口调试助手
  10. 短文阅读1:Entire Space Multi-Task Model: An Effective Approach for Estimating Post-Click Conversion Rate
  11. ffmpeg所有的解码器(decoders)
  12. 数据之美(五):美不胜收的数据图(上)
  13. 超低功耗离线智能语音识别芯片AT6811
  14. 脉冲信号matlab代码,MATLAB产生SNR可设的脉冲调制信号代码
  15. GBASE 8C——SQL参考6 sql语法(12)
  16. 计算机系400分左右的学校,杭州2021年400分能上计算机学校吗
  17. 用selenium 做个淘宝秒杀
  18. 赛灵思FPGA编程入门指南
  19. Conflux网络2022路线图
  20. 在macbook上安装windows几种方案

热门文章

  1. js下载文件及前端使用a标签下载文件download属性失效问题
  2. 【Cordova】Cordova第一个插件的创建与使用
  3. 【调剂】2022浙江工业大学宣琦课题组招收研究生(有调剂名额)
  4. 异步编程的实现方式以及区别
  5. 人物模型3d模型素材推荐 精品 小众
  6. ManualResetEvent使用
  7. 【论文笔记】VOLO: Vision Outlooker for Visual Recognition
  8. zumi 画师_【Zumi画师】【全彩无修】 1- 42期图集整合 18G
  9. 中国志愿者服务器注册,全国志愿服务信息系统操作指南二(志愿者注册)
  10. 全网最全json数据结构可视化工具汇总