簡體   English   中英

在dojo中,如何擦除內部文本節點而不刪除元素內的其他節點?

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

我有

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

我想抹掉“ blah”而不刪除blah2

我該怎么辦?

使用最新的dojo 1.10。

我不確定是否只能使用dojo來完成。
以下代碼混合使用了dojo普通的 javascript對象,以實現所需的結果。

您的問題陳述。(注意:我已向父div添加了id屬性“ mydiv”)。

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

刪除所有文本節點“ blah”。

// 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;
}
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM