六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 280|回复: 0

只有Firefox不支持insertAdjacentHTML和insertAdjacentText

[复制链接]

升级  60.33%

119

主题

119

主题

119

主题

举人

Rank: 3Rank: 3

积分
381
 楼主| 发表于 2013-1-29 13:50:54 | 显示全部楼层 |阅读模式
这两个属性首先是IE4提出且实现的,它可以实现在一个 DOM 元素的前面、后面、第一个子元素前面、最后一个子元素后面四个位置,插入指定的 HTML 代码其它浏览器相继支持。唯有Firefox不支持。insertAdjacentHTML已被HTML5采纳。如下扩展HTMLElement.prototype,为其增加这两个属性:
if(HTMLElement.prototype.insertAdjacentHTML == undefined) {    HTMLElement.prototype.insertAdjacentElement = function(where, node) {        switch (where) {            case "beforeBegin":                this.parentNode.insertBefore(node, this);break;            case "afterBegin":                this.insertBefore(node, this.firstChild);break;            case "beforeEnd":                this.appendChild(node);break;            case "afterEnd":                if (this.nextSibling)                    this.parentNode.insertBefore(node, this.nextSibling);                else                    this.parentNode.appendChild(node);                break;        }    }    HTMLElement.prototype.insertAdjacentHTML = function(where, html) {        var r = this.ownerDocument.createRange();        r.setStartBefore(this);        var parsedHTML = r.createContextualFragment(html);        this.insertAdjacentElement(where, parsedHTML);    }    HTMLElement.prototype.insertAdjacentText = function(where, txt) {        var parsedText = document.createTextNode(txt);        this.insertAdjacentElement(where, parsedText);    }} 
http://msdn.microsoft.com/en-us/library/ms536452(VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms536453(VS.85).aspx
http://www.w3.org/TR/html5/apis-in-html-documents.html#insertadjacenthtml
 
 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

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