2011-04-26 177 views
1

我有一個功能,下面的代碼:node.setAttribute導致INVALID_CHARACTER_ERR

this.HtmlRef = document.createElement("canvas"); 

    if (!this.HtmlRef) 
    { 
    return false; 
    } 

    this.HtmlRef.appendChild( document.createTextNode("Your browser doesn't support dynamic graphic drawings (Canvas)!") ); 

    var Width = document.createAttribute("width"); 
    var Height = document.createAttribute("height"); 
    Width.nodeValue = 0; 
    Height.nodeValue = 0; 

    this.HtmlRef.setAttribute(Width); /* error */ 
    this.HtmlRef.setAttribute(Height); /* error */ 

我也試圖改變寬度和高度,但沒有成功的名字! 整個錯誤信息:

Uncaught Error: INVALID_CHARACTER_ERR: DOM Exception 5

謝謝。

的解決方案是使用使用setAttributeNode代替的setAttribute

+0

您還可以,如果你使用'使用document.createElement('#文件看到這個錯誤')'(或用'createElementNS');如果你天真地嘗試克隆整個'Document'而不是根節點,就可能發生這種情況。解決方法是在'node.documentElement'(root)開始克隆。 – Phrogz 2011-11-02 20:21:20

回答

2

通過setAttributeNode()代替的setAttribute()

+0

它的工作,謝謝;) – ComFreek 2011-04-26 16:12:24