Skip to content

Instantly share code, notes, and snippets.

@itochan
Last active Nov 1, 2019
Embed
What would you like to do?
<!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