2011-07-26 44 views
2

私はtextContentに相当するIEが必要です。IEのためのJavascriptのtextContent(空白を保存するinnerText)

あなたのズボンの座席から飛び降りる前に、innerTextはtextContentと同じではありません。 innerTextは、DOMレンダリングが解析されるときに空白を解析します。 textContentは、書かれているように空白を解析します。私は後者が必要ですが、I.E. textContentをサポートしていません。例えば

は:

DOM Obj: <p>This is an example.</p> 
innerText: "This is an example." 
textContent: "This is an example." 

答えて

1

私はあなたができるとは思いません。 IEはTextNode#nodeValueプロパティ内でもスペースを「正規化」します。しばらく前に私はこれ書いた:

function textValue(element) { 
    var collector = []; 
    textValueCollector(element, collector); 
    return collector.join(""); 
} 
function textValueCollector(element, collector) { 
    var node; 
    for (node = element.firstChild; node; node = node.nextSibling) 
    { 
     switch (node.nodeType) { 
      case 3: // text 
      case 4: // cdata 
       collector.push(node.nodeValue); 
       break; 
      case 8: // comment 
       break; 
      case 1: // element 
       if (node.tagName == 'SCRIPT') { 
        break; 
       } 
       // FALL THROUGH TO DEFAULT 
      default: 
       // Descend 
       textValueCollector(node, collector); 
       break; 
     } 
    } 
} 

をそして私はノー、悲しげに、それはあなたが必要なものかもしれないと思ったが、。 this live exampleで見ることができるように、IE上でテキストノードを直接歩いていても余分なスペースは残されていないので、実際には61のところに45の長さしか表示されません(他のブラウザ[Chrome、Firefox、Opera ])。

関連する問題