English 中文(简体)
仅将案文节点建设成 j体
原标题:Get just the text nodes constructed into a string in javascript
<div>
some text I want
<span><sup>text I don t want</sup></span>
</div>

Above is some example html. I want to be able to select just the text of the div without the child html text that is located in the span. Is there any simple way of doing this or am I going to have to come up with some way to hack away at the markup?

我先尝试利用jquery 挑选人,只选择干 the,但我最后说话。 页: 1 我一无一无所知?

最佳回答

下面请你宣读案文。

>DEMO/strong>

var justText = $( div ).contents().filter(function () {
    if (this.nodeType == 3) return true;
}).text();

请注意,这还将使你回到分界线和白色空间。

您可使用<代码>$.trim,以删除这些编号。

justText = $.trim(justText);
问题回答

Try:

var text = $( div ).contents().get(0);

此处可参阅:。 http://jsfiddle.net/xHcPU/

//elem.childNodes is a NodeList containing all child nodes of the array
//This includes every node - text and elements
// https://developer.mozilla.org/En/DOM/Node.childNodes
var childNodes = document.getElementById(  special  ).childNodes;

//arr.reduce receives an array-like object and a callback
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/Reduce

//We use func.call to call arr.reduce with the childNodes as the this variable
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call
var out = [].reduce.call( childNodes, function ( ret, child ) {

    //if a node s nodeType is 3, you can safely say it s a text node
    // https://developer.mozilla.org/en/nodeType
    if ( child.nodeType === 3 ) {
        ret += child.nodeValue.trim();
    }
    //if it s any other node, we just ignore it
    return ret;

},    );

//out will now contain the string you seek

请注意,此处使用的2个ES5功能,Array.prototype.reduceString.prototype.trim,可以很容易地由下列人员取代:shim、您本人的履行,或如果有的话,则由j Query等值取代。 我不记得<条码>减去<>编码>等值,但我确实认为存在<条码>。

jQuery is completely unnecessary. Strip all HTML:

var text = node.innerHTML.replace(/<.+>/,   );

Demo with jQuery to select the div.

仅收集儿童节点案文:

function getImmediateText(el) {
  var node, nodes = el.childNodes;
  var text =   ;

  for (var i=0, iLen=nodes.length; i<iLen; i++) {
    node = nodes[i];

    if (node.nodeType == 3) {
      text += node.data;
    }
  }
  // You may want to trim excess white space here
  return text;
}

That will work in every browser and doesn t need library support.

尝试:

$( div ).clone().children().remove().end().text()




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!