如何从jquery对象中删除一个子节点但不更改html

时间:2017-01-05 04:09:35

标签: javascript jquery

这是我的代码:

$.fn.extend({
    del: function() {

    }
  })
  var ds = $(".d");
  ds.del(ds[0])
  console.log(ds.length)

我希望从jquery childs实现jquery.del到del子,但不能更改html,如何实现它?

更新

我可以实现var matchDs = ds.del(ds [0]),但我希望从原始jquery对象中获取del子元素的方法

1 个答案:

答案 0 :(得分:0)

我发现jquery对象有#splice func,所以我可以使用以下代码:

$.fn.extend({
    del: function (e) {
      for (var i = 0; i < this.length; i++) {
        if (this[i] == e) {
          this.splice(i, 1)
        }
      }
    }
  })
  var ds = $(".d");
  ds.del(ds[0])
  console.log(ds.length)