六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 43|回复: 0

FF下支持IE特有的insertAdjacentElement以及insertAdjacentHTML

[复制链接]

升级  60%

44

主题

44

主题

44

主题

秀才

Rank: 2

积分
140
 楼主| 发表于 2012-12-22 21:29:43 | 显示全部楼层 |阅读模式
<div id="cnblogs_post_body">网摘:
在ie下这连个函数主要是向目标元素对象插入一个新的子元素,子元素可以使element也可以是html代码片段
由于FF中没有这两个函数,因此我们可以通过扩展HTMLElement来解决这个问题:
<div class="cnblogs_code">if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement){     HTMLElement.prototype.insertAdjacentElement = function(where,parsedNode)     {        switch (where)        {            case 'beforeBegin':                this.parentNode.insertBefore(parsedNode,this)                break;            case 'afterBegin':                this.insertBefore(parsedNode,this.firstChild);                break;            case 'beforeEnd':                this.appendChild(parsedNode);                break;            case 'afterEnd':                if (this.nextSibling) this.parentNode.insertBefore(parsedNode,this.nextSibling);                    else this.parentNode.appendChild(parsedNode);                break;         }     }     HTMLElement.prototype.insertAdjacentHTML = function (where,htmlStr)     {         var r = this.ownerDocument.createRange();         r.setStartBefore(this);         var parsedHTML = r.createContextualFragment(htmlStr);         this.insertAdjacentElement(where,parsedHTML)     }     HTMLElement.prototype.insertAdjacentText = function (where,txtStr)     {         var parsedText = document.createTextNode(txtStr)         this.insertAdjacentElement(where,parsedText)     }}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表