在 dojo 中,我将如何擦除内部文本节点而不擦除元素内的其他节点

In dojo, how would I erase an inner text node without erasing other nodes inside the element also?

本文关键字:节点 擦除 dojo 文本 元素 其他 内部 何擦除      更新时间:2023-09-26

我有

<div>
    blah
    <div>blah2</div>
</div>

我想在不擦除 blah2 的情况下擦除"等等"

我该怎么做?

使用最新的道场 1.10。

我不确定是否可以使用道场来完成。
下面的代码混合使用 dojo普通 javascript 对象来实现您想要的结果。

您的问题陈述。(注意:我已经在父div 中添加了一个 id 属性"mydiv")。

<div id="mydiv">
    blah
    <div>blah2</div>
</div>

删除所有文本节点"等等"。

// require the query and domReady modules
require(["dojo","dojo/query", "dojo/domReady!" ], function(dojo,query) {
   // retrieve an array of nodes with the ID "list"
   var list = query("#mydiv")[0];
   console.log("list:",list);
   var childNodes = list.childNodes;
   var len = childNodes.length;
   var i;
   for ( i = 0; i < len; i++){
       // Destroy All textnodes.
       if ( childNodes[i].nodeType === 3 ) {
          //console.log ("Text node found");
          dojo.destroy(childNodes[i]);
       };
   } 
})

由 Tim Down 提供:如何使用 jquery 从父元素中删除文本(不删除内部元素) 据他说,这应该从 IE5 开始工作。

require(["dojo/query", "dojo/domReady!"], function(query) {
var parent = query("#parentid")[0];    
var nextchild;
var child = parent.firstChild;
while (child) {
nextChild = child.nextSibling;
if (child.nodeType == 3) {
parent.removeChild(child);
}
child = nextChild;
}
});