相容瀏覽器的insertAdjacentHTML

來源:互聯網
上載者:User

標籤:瀏覽器   資料   而且   


添加HTML內容與常值內容以前用的是innerHTML與innerText方法,

最近發現還有insertAdjacentHTML和 insertAdjacentText方法,

這兩個方法更靈活,可以在指定的地方插入html內容和常值內容。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

參數資料:

msdn.microsoft.com/en-us/library/ms536452(VS.85).aspx

www.w3.org/TR/2000/REC-DOM-Level-2-Traversal-Range-20001113/ranges.html#Level2-Range-setStartBefore

https://developer.mozilla.org/en/DOM/range.createContextualFragment

www.never-online.net/blog/article.asp?id=113

 

首先是IE裡有 insertAdjacentHTML,而且很好用。為了相容其它瀏覽器就有了如下的函數“insertHTML”

重點:

1、為了相容其它瀏覽器,支援beforeBegin、afterBegin、beforeEnd、afterEnd

2、如何將字串String轉換為文檔片段(fragment)或是Dom樹(Dom tree),因為使用innerHTML無法指定位置。

 

將字串轉換為Dom tree,參數這裡:range.createContextualFragment,contextual翻譯過來好像是“語境”的意思??

 

range的起始位置的設定,參數setStartBefore、setStartAfter

 

下面就是完整的代碼了(IE中可根據需要是否返回附加的dom):

完整的程式碼範例:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html> <head>  <title>相容瀏覽器的insertAdjacentHTML</title>  <meta name="generator" content="editplus" />  <meta name="author" content="" />  <meta name="keywords" content="" />  <meta name="description" content="" />  <meta http-equiv=‘content-type‘ content=‘text/htm;charset=utf-8‘ /> <style type="text/css">* {margin:0; padding:0;}body {background-color:#fff}dl, dd {margin:20px;} .color_1 {color:#F7941D; border:1px solid #F7941D;}.color_2 {color:#f00; border:1px solid #f00;}.color_3 {color:#01FE34; border:1px solid #01FE34;}.color_4 {color:#F49AC1; border:1px solid #F49AC1;}.color_5 {color:#00AEEF; border:1px solid #00AEEF;}.color_6 {color:#DB00EF; border:1px solid #DB00EF;}.color_7 {color:#00CCFD; border:1px solid #00CCFD;}</style> </head>  <body> <script type=‘text/javascript‘> /** * * * @param {HTMLElement} el * @param {String} where beforeBegin、afterBegin、beforeEnd、afterEnd * @param {String} html */function insertHTML(el, where, html) {    if (!el) {        return false;    }         where = where.toLowerCase();         if (el.insertAdjacentHTML) {//IE        el.insertAdjacentHTML(where, html);    } else {        var range = el.ownerDocument.createRange(),            frag = null;                 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);                } else {                    el.innerHTML = html;                }                return el.firstChild;            case "beforeend":                if (el.lastChild) {                    range.setStartAfter(el.lastChild);                    frag = range.createContextualFragment(html);                    el.appendChild(frag);                } 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;        }    }} function btnHandler() {    var elem = document.getElementById(‘abc‘);         insertHTML(elem, ‘beforeBegin‘, ‘<dd class="color_2">上一個兄弟節點previousSibling</dd>‘);    insertHTML(elem, ‘beforeEnd‘, ‘<dd class="color_3">最後一個節點lastChild</dd>‘);    insertHTML(elem, ‘afterBegin‘, ‘<dd class="color_4">第一個節點firstChild</dd>‘);    insertHTML(elem, ‘afterEnd‘, ‘<dd class="color_5">下一個兄弟節點nextSibling</dd>‘);}</script><button onclick="btnHandler();">insertAdjacentHTML</button><dl class="color_1" id="abc">    <dd><a href="#">今天採訪出來,天上下雨,那個偏僻的地方根本沒出租車的影子</a></dd>    <dd><a href="#">今天採訪出來,天上下雨,那個偏僻的地方根本沒出租車的影子</a></dd>    <dd><a href="#">今天採訪出來,天上下雨,那個偏僻的地方根本沒出租車的影子</a></dd>    <dd><a href="#">今天採訪出來,天上下雨,那個偏僻的地方根本沒出租車的影子</a></dd>    <dd><a href="#">今天採訪出來,天上下雨,那個偏僻的地方根本沒出租車的影子</a></dd>  </dl> </body></html>



<轉載自:http://www.cnblogs.com/meteoric_cry/archive/2010/10/14/1851093.html>

相容瀏覽器的insertAdjacentHTML

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.