2011-08-19 48 views
2

我想克隆一個HTML節點使用瀏覽器的DOM API的cloneNode()方法,甚至使用Jquery clone()函數。這個API可以很好的處理HTML標籤,但是我在使用HTML5標籤時遇到了一些問題,例如時間,在IE中的HTML5節點克隆節點問題

的問題是,以下<time>標籤內容<time class="storydate">April 7, 2010</time>被轉換爲:<:時間CLASS = storydate awpUniq =「912」 > 2010年4月7日,儘管IE提供原始時間節點正確的,那麼爲什麼這樣的問題,與克隆API 。

而這個問題在FF/chrome中沒有被觀察到。請提供一些線索如何避免這種情況

+0

冒號表示命名空間,你使用XHTML文檔類型?這一切似乎都適用於IE8與HTML5文檔類型。此外,[這可能是有用的](http://debeterevormgever.nl/en/articles/html5-elements-ie-without-javascript)。 – robertc

回答

3

這有什麼幫助嗎?從HTML5希夫問題列表:

http://code.google.com/p/html5shiv/issues/detail?id=28

鏈接

http://pastie.org/935834#49

的解決方案似乎是:

// Issue: <HTML5_elements> become <:HTML5_elements> when element is cloneNode'd 
// Solution: use an alternate cloneNode function, the default is broken and should not be used in IE anyway (for example: it should not clone events) 

// Example of HTML5-safe element cloning 
function html5_cloneNode(element) { 
    var div = html5_createElement('div'); // create a HTML5-safe element 

    div.innerHTML = element.outerHTML; // set HTML5-safe element's innerHTML as input element's outerHTML 

    return div.firstChild; // return HTML5-safe element's first child, which is an outerHTML clone of the input element 
} // critique: function could be written more cross-browser friendly? 
+0

這對於DOM元素很適用,但是我會丟失與DOM節點相關的數據。即node.data(「prop」,value); – hjindal