0
4

2 回答 2

0

一种粗暴的方法,但您可以取出您希望在第一个节点中出现的所有标签并读取直到下一个标签的开始,如下所示:

text = text.replace("<b>"," ");
text = text.replace("</b>"," ");
text = text.replace("<i>"," ");
text = text.replace("</i>"," ");
text = text.replace("<span>"," ");
text = text.replace("</span>"," ");

text = text.substr(0, text.indexOf("<"));
于 2013-01-23T09:37:15.247 回答
0

我没有完全遵循这个问题,但是如果您尝试从 DOM 元素中提取文本,这可能会有所帮助:

  var getText = function (el) {
    var ret;
    var txt = [],
      i = 0;

    if (!el) {
      ret = "";
    } else if (el.nodeType === 3) {
      // No problem if it's a text node
      ret = el.nodeValue;
    } else {
      // If there is more to it, then let's gather it all.
      while (el.childNodes[i]) {
        txt[txt.length] = getText(el.childNodes[i]);
        i++;
      }
      // return the array as a string
      ret = txt.join("");
    }
    return ret;
  };
于 2013-01-24T10:39:32.593 回答