3

I'm trying to remove all child elements from a node but leave the actual text content of the node. I.e. go from this:

<h3>
   MY TEXT
   <a href='...'>Link</a>
   <a href='...'>Link</a>
   <select>
      <option>Value</option>
      <option>Value</option>
   </select>
</h3>

to this:

<h3>
   MY TEXT
</h3>

I know that there are a million easy ways to do this in jQuery, but it's not an option for this project... I've got to use plain old javascript.

This:

var obj = document.getElementById("myID");
if ( obj.hasChildNodes() ){
    while ( obj.childNodes){
        obj.removeChild( obj.firstChild );       
    }
}

obviously results in just <h3></h3>, and when I tried:

var h3 = content_block.getElementsByTagName('h3')[0];
var h3_children = h3.getElementsByTagName('*');
for(var i=0;i<h3_children.length;i++){
   h3_children[i].parentNode.removeChild(h3_children[i]);
}

It gets hung up part way through. I figured it was having trouble removing the options, but altering the for loop to skip removal unless h3_children[i].parentNode==h3 (i.e. only remove first-level child-elements) stops after removing the first <a> element.

I'm sure I'm missing something super obvious here, but perhaps it's time to turn to the crowd. How can I remove all child elements but leave the first-level textNodes alone? And why doesn't the above approach work?

EDITS

There are a couple of working solutions posted, which is great, but I'm still a little mystified as to why looping through and removing h3.getElementsByTagName('*') doesn't work. A similar approach(adapted from Blender) likewise does not complete the process of removing child nodes. Any thoughts as to why this would be?

4

5 に答える 5

3
var h3=content_block.getElementsByTagName("h3")[0];
for(var i=0;i<h3.childNodes.length;i++)
{
  if(h3.childNodes[i].nodeType==3)//TEXT_NODE
  {
    continue;
  }
  else
  {
    h3.removeChild(h3.childNodes[i]);
    i--;
  }
}

JSFiddle デモ

編集

を組み合わせて、i--短く見えるようにします。

var h3=content_block.getElementsByTagName("h3")[0];
for(var i=0;i<h3.childNodes.length;i++)
{
  if(h3.childNodes[i].nodeType==3)//TEXT_NODE
    continue;
  else
    h3.removeChild(h3.childNodes[i--]);
}

編集#2:

@SomeGuy によって指摘されたので、さらに短くします。

var h3=content_block.getElementsByTagName("h3")[0];
for(var i=0;i<h3.childNodes.length;i++)
{
  if(h3.childNodes[i].nodeType!=3)//not TEXT_NODE
    h3.removeChild(h3.childNodes[i--]);
}

ブラケットも削除できますが、それは「読みにくく」、「混乱」するため、そのままにしておきます。

于 2012-10-19T05:50:26.820 に答える
1

プロパティ.nodeTypeまたは.nodeNameノードごとに確認できます。

テキスト ノードには、これらのプロパティが次のように設定されています。

.nodeType == 3
.nodeName == '#text'`
于 2012-10-19T05:54:45.883 に答える
1

例えば:

var e = obj.firstChild
while (e) {
   if (e.nodeType == 3) {
      e = e.nextSibling
   } else {
      var n = e.nextSibling
      obj.removeChild(e)
      e = n
   }
}
于 2012-10-19T05:57:51.027 に答える
1

これを試して。ここにアリのテキストを保持すると仮定します。

var h3 = document.getElementsByTagName('h3')[0];

if (h3.hasChildNodes()) {
    for (var i = h3.childNodes.length - 1; i >= 0; i--) {
        if (h3.childNodes[i].nodeName != "#text")
            h3.removeChild(h3.childNodes[i]);
    }
}

それがうまくいくことを願っています。

于 2012-10-19T06:02:36.037 に答える
0

さて、私が使用したもの(ここの回答に触発されたもの)は、次のようなものです:

   var h3 = document.getElementsByTagName("h3")[0];
    Array.protoype.filter.call(h3.childNodes, function(child){
        if (child.nodeType != 3) {
            h3.removeChild(child);
        }
    });
于 2014-11-06T10:00:13.833 に答える