从DOM中删除元素

w.b*_*ian 1 dart

我正在创建一个div元素并将其添加到body元素,然后尝试删除该元素.这个缩写代码说明了我正在尝试做的事情:

//create div
_backdropDiv = new DivElement();

//add div to body, this works as expected
window.document.query('body').elements.add(_backdropDiv);

//in some other method...
var body = window.document.query('body');

//it's odd the List<E> doesn't specify a remove method, we'll jump through some hoops...
var backdropIndex = body.elements.indexOf(_modalDiv);
body.elements.removeRange(backdropIndex, 1); //<--- NotImplementedException
Run Code Online (Sandbox Code Playgroud)

因此,从DOM中删除此元素的最明显方法不起作用,因为未实现removeRange.我应该另辟蹊径吗?

在一个不相关的说明,为什么没有remove()指定方法List<E>?必须执行两个操作(indexOf(),removeRange())似乎很笨拙.

Chr*_*ett 6

答案并不明显,但看似简单.Node接口(Element扩展)有一个remove()方法可以将它从DOM中删除.

_backdropDiv.remove();
Run Code Online (Sandbox Code Playgroud)

参考:http://api.dartlang.org/html/Node.html#remove