Skip to content

Instantly share code, notes, and snippets.

@itochan
Last active November 1, 2019 08:04
Show Gist options
  • Save itochan/6a8c4b95c0ab786f825350e549a48d29 to your computer and use it in GitHub Desktop.
Save itochan/6a8c4b95c0ab786f825350e549a48d29 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>演習5-1 配列操作とHTMLの一括操作</title>
<script src="ex05-1.js"></script>
</head>
<body>
<input type="button" value="数える" onclick="countElements();">
<input type="button" value="倍" onclick="doubleElements();">
<ol id="list">
<li>liタグだよ</li>
</ol>
</body>
</html>
function countElements() {
var elements = document.getElementsByTagName("li");
alert(elements.length);
}
function doubleElements() {
var elements = document.getElementsByTagName("li");
var length = elements.length;
var list = document.getElementById("list");
var element = "<li>liタグだよ</li>";
list.innerHTML = "";
for(var i = 0; i < length * 2; i++) {
list.innerHTML += element;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment