当我添加一些<select>
例如SELECT_country, SELECT_country1, SELECT_country2
之后,我想按照它们出现的顺序,从最新的一个开始删除直到最老的。但实际上是从最老的一个到最新的一个删除。我以为SELECT_country
是父元素,我将要删除它的子元素,但实际上是父元素先消失了。
我该怎么改变这个呢?
var j = 0;
function add_country() {
j++;
var select = document.getElementById("SELECT_country");
var clone = select.cloneNode(true);
clone.setAttribute("id", "SELECT_country" + j);
clone.setAttribute("name", "country" + j);
document.getElementById("DIV_country").appendChild(clone);
}
function remove_country() {
var select = document.getElementById('SELECT_country');
select.parentNode.removeChild(select);
}
select.lastChild.parentNode
不总是等于select
吗? - Dan D.lastElementChild
。 - dakab