我有一個代碼如下div標記陣列內標識元件唯一地
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Accordion - Default functionality</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
$("#accordion").accordion();
});
var i = 1;
function pressMe() {
var h3 = document.createElement("h3"),
div = document.createElement("div");
h3.appendChild(document.createTextNode('Section 4'));
div.appendChild(document.createTextNode('Saurabh'));
// note the reverse order of adding child
h3.appendChild(div);
//document.getElementById('accordion').appendChild(h3);
//$("#accordion").accordion();
$("#accordion").append('<h3>Section ' + ++i + '</h3> <div id=\'myDiv' + i + '\'>' + document.getElementById('temp').innerHTML + '</div>');
$("#accordion").accordion("refresh");
}
function hide() {
document.getElementById('temp').style.display = "none";
}
function printAll() {
for (temp = 2; temp <= i; temp++) {
alert(document.getElementById('myDiv' + temp).getElementsByTagName('input')[0].value);
}
}
</script>
</head>
<body id="XXX" onload="hide()">
<div id="accordion">
<h3>Section 1</h3>
<div id='myDiv1'>
<input type="text" name="text1" id="text1">
</div>
</div>
<input type="button" onclick="pressMe()" value="Add more Nodes">
<input type="button" onclick="printAll()" value="printAll">
<div>
<div id='temp'>
<input type="text" name="text1" id="id_text">
</div>
</div>
</body>
所有我想這樣做我想以唯一地識別所有的輸入由
document.getElementById('myDiv' + temp).getElementsByTagName('input')[0].value
未標記
但使用
document.getElementById('myDiv' + temp).getElementsById('id_text')[0].value
但無法檢索元素。
沒有我稱爲'getElementsById'。爲什麼不只是'getElementById(「id_text」)',因爲ID在頁面中必須是唯一的? – Beterraba