【问题标题】:What's the difference between `document.createTextNode` and `new Text()`?`document.createTextNode` 和 `new Text()` 有什么区别?
【发布时间】:2022-07-21 12:15:06
【问题描述】:

两者都创建一个新的Text 节点。似乎唯一的区别是浏览器兼容性。还有其他区别吗?

【问题讨论】:

    标签: javascript dom


    【解决方案1】:

    看起来,根据 DOM 标准,几乎没有区别:

    text = new Text([data = ""])
    

    返回一个新的文本节点,其数据是数据。 (link)

    text = document . createTextNode(data)
    

    返回一个数据为数据的文本节点。 (link)

    除了参数对于构造函数是可选的,但对于createTextNode 是必需的。 (省略createTextNode 的参数将抛出。)

    new Text();
    console.log('first line worked');
    document.createTextNode(); // throws

    【讨论】:

      猜你喜欢
      • 2022-12-04
      • 2019-08-24
      • 2018-08-02
      • 2011-11-22
      • 1970-01-01
      • 2022-01-21
      • 2017-10-31
      • 2018-06-04
      • 2012-12-29
      相关资源
      最近更新 更多