当前位置:   jquery > 正文

JS删除最后一个孩子?

javascript,https,DevBox,在线流程图,编程,编程问答,程序员,开发者工具,开发工具,json解析,二维码生成,unix时间戳,在线开发工具,前端开发工具,开发人员工具,站长工具

当我添加一些> select> ex.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);
}

Miguel Mota.. 17

由于要附加新元素,因此lastChild如果要将它们从最新到最旧删除,则需要使用最后一个元素.

function remove_country() {
  var select = document.getElementById('DIV_country');
  select.removeChild(select.lastChild);
}

JSFiddle演示:https://jsfiddle.net/rrkroxcb/1/



1> Miguel Mota..:

由于要附加新元素,因此lastChild如果要将它们从最新到最旧删除,则需要使用最后一个元素.

function remove_country() {
  var select = document.getElementById('DIV_country');
  select.removeChild(select.lastChild);
}

JSFiddle演示:https://jsfiddle.net/rrkroxcb/1/

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/jquery/detail/13457
推荐阅读
相关标签
  

闽ICP备14008679号