【问题标题】:node.setAttribute causes INVALID_CHARACTER_ERRnode.setAttribute 导致 INVALID_CHARACTER_ERR
【发布时间】:2011-04-27 00:14:05
【问题描述】:

我在函数中有以下代码:

  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 */

我也尝试更改宽度和高度的名称,但没有成功! 整个错误信息:

未捕获的错误:INVALID_CHARACTER_ERR:DOM 异常 5

谢谢。

解决办法是用setAttributeNode代替setAttribute

【问题讨论】:

  • 如果你使用document.createElement('#document')(或createElementNS)也可以看到这个错误;如果您天真地尝试克隆整个 Document 而不是根节点,则可能会发生这种情况。解决方案是从node.documentElement(根目录)开始克隆。

标签: javascript html dom canvas setattribute


【解决方案1】:

使用 setAttributeNode() 代替 setAttribute()

【讨论】:

    猜你喜欢
    • 2016-07-29
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-09
    • 2023-03-08
    • 1970-01-01
    相关资源
    最近更新 更多