javascript - node.setAttribute 导致 INVALID_CHARACTER_ERR

标签 javascript html dom canvas setattribute

我在函数中有以下代码:

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

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

Uncaught Error: INVALID_CHARACTER_ERR: DOM Exception 5

谢谢。

解决方案是使用 setAttributeNode 而不是 setAttribute

最佳答案

使用 setAttributeNode() 而不是 setAttribute()

关于javascript - node.setAttribute 导致 INVALID_CHARACTER_ERR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5793116/

相关文章:

javascript - 在node.js中使用 Electron 时如何读取元素的innerHTML?

Javascript .forEach() 不适用于 DOM 节点,因为它们会在此过程中发生变化

javascript不会执行但会打印代码

javascript - 如何浏览图片并在浏览器中显示?

html - 向右浮动在 Google Chrome 中不起作用

javascript - 如何优化canvas图像并获取jpg而不是png

JavaScript 语法错误

javascript - 这个例子中 @include 的 CSS 用法?我可以用@import 替换它们吗?

javascript - Css 重置为小于 320px

html - 我在输入文本中的图标字体工作正常,除了 Internet Explorer