【发布时间】:2014-02-16 15:23:12
【问题描述】:
我开始在 W3schools 中阅读 JavaScript,并在他们提供的示例中测试/更改了一些内容,以便我可以看到什么在做什么,但还没有设法识别语法。
下面是修改p标签内容的原代码,link改成它。
<p id="demo">
JavaScript can change the content of an HTML element.
</p>
<script>
function myFunction()
{
x = document.getElementById("demo"); // Find the element
x.innerHTML = "Hello JavaScript!"; // Change the content
}
</script>
<button type="button" onclick="myFunction()">Click Me!</button>
我想知道如何更改同一个类的内容,但失败了,因为您可以看到下面的示例不起作用。 Fiddle of code below.
<p class="demo">
JavaScript can change the content of an HTML element.
</p>
<p class="demo">Yolo</p>
<script>
function myFunction()
{
x = document.getElementsByClassName("demo"); // Find the element
x.innerHTML = "Hello JavaScript!"; // Change the content
}
</script>
<button type="button" onclick="myFunction()">Click Me!</button>
如果您能告诉我如何 ^^" 并帮助我理解,“getElementById”是一个可以是其他变量还是命令?
【问题讨论】:
标签: javascript html onclick tags