我正在使用jQuery的.each()循环一些元素,并且我想在满足特定要求时删除节点.我不确定这个的语法,我尝试了几件事:
$(this).remove();
$xml.remove(this);
$xml.removeNode(this);
Run Code Online (Sandbox Code Playgroud)
...等等.找到一个有效的例子有些困难,有人能指出我正确的方向吗?我假设它很简单,我只是找不到正确的语法.下面有一个不工作的小提琴.
谢谢
那是因为调用remove()只从DOM中删除了元素.它不会从$siblingsjQuery对象中删除它.
如果find()再次调用以在删除alex节点后重新匹配兄弟节点,您将获得预期的结果:
var $xml = $(xmlString);
var $siblings = $xml.find("sibling");
$siblings.each(function(){
var name = $(this).attr("name");
if (name == "alex")
$(this).remove();
});
// Here, the "alex" element is not in the DOM, but still part of $siblings.
$xml.find("sibling").each(function(){
var name = $(this).attr("name");
console.log(name);
});
// Since we're calling find() again, the code above will only print "bob".
Run Code Online (Sandbox Code Playgroud)
你会在这里找到一个更新的小提琴.
| 归档时间: |
|
| 查看次数: |
7344 次 |
| 最近记录: |