In particular why does document.getElementsById work here
<div id="move">add padding</div>
<button type="button" onclick="movefun()">pad</button>
<script>
function movefun() {
document.getElementById("move").style.paddingLeft = "50px";
}
</script>
but document.getElementsByClassName does not work
<div class="move">add padding</div>
<button type="button" onclick="movefun()">Set left padding</button>
<script>
function movefun() {
document.getElementsByClassName("move").style.paddingLeft = "50px";
}
</script>
I have left out the common things like the html and body tag to cut down on code length.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…