【问题标题】:How can I write output with javascript?如何使用 javascript 编写输出?
【发布时间】:2015-12-11 20:05:01
【问题描述】:

我创建了一个带有代码的 jsfiddle,该代码旨在将段落中的字符串替换为存储在按钮中的字符串。据我所知,这是正确的。但我无法让它工作。谁能指出我的错误?

<p id="arbitraryIdentification">Product information</p>
<button onclick="document.getElementByID('arbitraryIdentification').innerHTML = 'this text just replaced the original text'">text swaperizer</button>

http://jsfiddle.net/q8x9oupn/1/

【问题讨论】:

  • 大小写很重要,是getElementById 不是 getElementByID
  • 学习使用开发者控制台。它会给你错误。
  • 并且不要在 plunker 中放入不合适的 id 名称
  • jonasnas -- 你的意思是当你快速打字时你不使用种族诽谤作为占位符?

标签: javascript string syntax output


【解决方案1】:

看看函数document.getElementById

通过 ID 返回对元素的引用; ID 是一个 string 可用于标识元素

所以,你正确的代码是:

document.getElementById('unique-key-here')

Source

【讨论】:

    【解决方案2】:

    您需要将 id 括在引号和 getElementById(不是 ID)中

    http://jsfiddle.net/q8x9oupn/3/

    【讨论】:

      【解决方案3】:
      1. 'getElementByID' 应该是 'getElementById'('Id' 而不是 'ID')

        <button onClick="document.getElementById('arbitraryIdentification').innerHTML = 'this text just replaced the original text'">
        </button>
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-19
        • 2022-11-29
        • 1970-01-01
        • 2011-05-25
        • 1970-01-01
        • 2019-10-09
        • 1970-01-01
        • 2012-08-29
        相关资源
        最近更新 更多