如何使用 javascript 写入输出?

How can I write output with javascript?

我创建了一个 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/

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

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

看看函数document.getElementById

Returns a reference to the element by its ID; the ID is a string which can be used to identify the element

因此,您的正确代码是:

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

Source

  1. 'getElementByID' 应该是 'getElementById'('Id' 而不是 'ID')

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