我有以下的HTML -遞歸掃描DOM元素 - 使用Javascript
<a>
<b>
....
.....
<input type="button" name="add" onclick="..." value="add another"/>
</d>
</b>
....
</a>
我用的是下面的js snippets-
/**
* Dynamically add a remove button on next to the add button.
*
*/
addRemoveButton = function(node) {
if(node.nodeType == 3) {
if(node.nodeName == "input") {
if(node.getAttribute("type") == "button") {
if(node.getAttribute("name") == "add") {
var removeButton = node.cloneNode(true);
removeButton.removeAttribute("name");
removeButton.setAttribute("value", "remove");
removeButton.setAttribute("onclick", "");
removeButton.setAttribute("id", "");
(node.parentNode).appendChild(removeButton);
return;
}
}
}
}
if(node.nodeType == 1) {
var list = node.childNodes;
var i = 0;
while(i<list.length) {
return addRemoveButton(list[i]);
i++;
}
}
return;
}
現在我想補充型按鈕的輸入,(刪除按鈕),在上面列表中顯示的當前按鈕旁邊。我試圖遞歸地做到這一點。但是這不起作用。你能在上面的代碼中找到問題嗎?
顯示OP做錯了什麼是好的,但像jQuery這樣的庫,你不需要編寫那種代碼 – 2012-01-13 08:10:04
感謝所有的幫助。 – Acn 2012-01-13 08:10:46
@JuanMendes - 我同意。這就是爲什麼我也寫了一個jQuery版本。 – jfriend00 2012-01-13 08:12:27