`
deng131
  • 浏览: 678449 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

insertAdjacentHTML与insertAdjacentText

阅读更多
insertAdjacentHTML是IE中的特有DOM方法但其不能应用于FIREFOX。

insertAdjacentHTML和insertAdjacentText
insertAdjacentHTML方法:在指定的地方插入html标签语句。

原型:insertAdjacentHTML(swhere,stext)
参数: swhere:指定插入html标签语句的地方:              
 1.beforeBegin:插入到标签开始前 
 2.afterBegin:插入到标签开始标记后 
 3.beforeEnd:插入到标签结束标记前 
 4.afterEnd:插入到标签结束标记后 
 


为了兼容不同浏览器借用EXT中方法:
function insertHtml(where, el, html){      
     
    where = where.toLowerCase();      
    if(el.insertAdjacentHTML){      
     
        switch(where){      
            case "beforebegin":      
                el.insertAdjacentHTML('BeforeBegin', html);      
                return el.previousSibling;      
            case "afterbegin":      
                el.insertAdjacentHTML('AfterBegin', html);      
                return el.firstChild;      
            case "beforeend":      
                el.insertAdjacentHTML('BeforeEnd', html);      
                return el.lastChild;      
            case "afterend":      
                el.insertAdjacentHTML('AfterEnd', html);      
                return el.nextSibling;      
        }      
        throw 'Illegal insertion point -> "' + where + '"';      
    }      
                    
    var range = el.ownerDocument.createRange();      
    var frag;      
    switch(where){      
         case "beforebegin":      
            range.setStartBefore(el);      
            frag = range.createContextualFragment(html);      
            el.parentNode.insertBefore(frag, el);      
            return el.previousSibling;      
         case "afterbegin":      
            if(el.firstChild){      
                range.setStartBefore(el.firstChild);      
                frag = range.createContextualFragment(html);      
                el.insertBefore(frag, el.firstChild);      
                return el.firstChild;      
             }else{      
                el.innerHTML = html;      
                return el.firstChild;      
             }      
        case "beforeend":      
            if(el.lastChild){      
                range.setStartAfter(el.lastChild);      
                frag = range.createContextualFragment(html);      
                el.appendChild(frag);      
                return el.lastChild;      
            }else{      
                el.innerHTML = html;      
                return el.lastChild;      
            }      
        case "afterend":      
            range.setStartAfter(el);      
            frag = range.createContextualFragment(html);      
            el.parentNode.insertBefore(frag, el.nextSibling);      
            return el.nextSibling;      
    }      
    throw 'Illegal insertion point -> "' + where + '"';      
}     



参考:
http://www.cnitblog.com/yemoo/archive/2007/10/11/34711.html
http://ejohn.org/blog/dom-insertadjacenthtml/
分享到:
评论

相关推荐

    innertext , insertadjacentelement , insertadjacenthtml , insertadjacenttext 等区别

    类似的像 insertAdjacentElement , insertAdjacentElement , insertAdjacentHTML , insertAdjacentText 等。如果需要使用这些非标准的方法,或者已有的代码大量使用了这些方法的话,就必须为其他浏览器提供...

    insertAdjacentHTML动态插入行[归类].pdf

    此外,还有一个与`insertAdjacentHTML`相似但只能插入纯文本的方法`insertAdjacentText`,它的用法与`insertAdjacentHTML`类似,只是插入的内容不会被解析为HTML。 最后,一个简单的动态添加和删除元素的例子: ``...

    如何在指定的地方插入html内容和文本内容

    与`innerHTML`不同,`innerHTML`会替换元素内所有子节点,而`insertAdjacentHTML`和`insertAdjacentText`则是在原有内容的基础上进行添加。 此外,`innerHTML` 属性是可读写的,用于获取或设置元素的HTML内容。例如...

    js对各标签(控件)的操作

    在 Web 开发中,JavaScript 作为一种强大的客户端脚本语言,被广泛用于实现网页的动态效果与交互性。通过 JavaScript,开发者可以轻松地操作 HTML 文档中的各种元素(标签),从而实现丰富的用户体验。本文将详细...

    javaScript重要知识

    使用`insertAdjacentHTML()`或`insertAdjacentText()`方法,可以在DOM元素的前后插入内容。 **示例代码:** ```javascript document.body.insertAdjacentHTML("BeforeBegin", "要在元素前插入的内容"); document....

    js常用操作

    - `insertAdjacentHTML` 和 `insertAdjacentText`:用于在指定位置插入 HTML 或文本。 #### 字符串处理 字符串是 JavaScript 中常见的数据类型,用于处理文本。 - `lastIndexOf` 和 `indexOf`:用于查找子字符串...

    jquery 追加元素append、prepend、before、after用法与区别分析

    示例代码中,`_insert()`函数展示了如何在DOM操作中使用`insertAdjacentHTML()`、`insertAdjacentText()`和`insertAdjacentElement()`等原生JavaScript方法。`append()`、`prepend()`、`before()`和`after()`等...

    总结AJAX相关JS代码片段和浏览器模型

    - `insertAdjacentElement`,`insertAdjacentHTML`,`insertAdjacentText`:在指定位置插入元素、HTML或文本。 - `setAttribute`:设置元素的属性值。 - `removeChild`:删除子节点。 掌握这些基本操作后,开发者...

Global site tag (gtag.js) - Google Analytics