代码之家  ›  专栏  ›  技术社区  ›  Raja

如何获取不属于jquery中任何其他容器的DIV的文本?

  •  14
  • Raja  · 技术社区  · 15 年前

    这应该很容易。下面给出的是HTML。

    <div id='attachmentContainer'>
        #Attachment#
        <span id='spnAttachmentName' class='hidden'>#AttachmentName#</span>
        <span id='spnAttachmentPath' class='hidden'>#AttachmentPath#</span>
    </div>  
    

    我只想得到附件,而不是其他文本。当我尝试的时候

    $("#attachmentContainer").text() 
    

    它给出了所有的附件名以及附件路径。我知道我可以把附件放到另一个范围内,直接访问它,但我对如何这样做很感兴趣。任何帮助都非常感谢。

    6 回复  |  直到 11 年前
        1
  •  11
  •   Tomalak    15 年前

    因为您的文本恰好是 <div> :

    var firstChild = $("#attachmentContainer")[0].firstChild;
    var textValue  = firstChild.nodeType == 3 ? $.trim(firstChild.nodeValue) : "";
    

    这个 nodeType 检查是一种保护措施-它确保您实际上正在处理一个文本节点- firstChild 毕竟可能有些不同。因此,这只是一个例子。

    要检索所有文本子级(或特定子级)的值,只需循环 childNodes 元素集合,将找到的所有位连接到字符串中:

    // the optional "at" parameter lets you define which text node you want
    // if not given, this returns all text nodes concatenated
    $.fn.ownText = function(at) { 
      var result = [], node = this[0];
      if (!(node && node.childNodes)) return;
      for (var i=0; i<node.childNodes.length; i++) {
        var child = node.childNodes[i];
        if (child.nodeType != 3) continue;
        var t = $.trim(child.nodeValue);
        if (t != '') result.push(t);
      }
      return at ? result[at-1] : result.join(' ');
    }
    
    var text = $("#attachmentContainer").ownText();  // all text children
    var text = $("#attachmentContainer").ownText(1); // first text child only
    
        2
  •  6
  •   hunter    15 年前

    这只会让你得到项目文本

    var $item = $("#attachmentContainer").clone();
    $item.children().remove(); 
    alert($item.text());
    

    克隆对象,这样就不必删除实际的子项。然后,您可以删除子元素,这样将保留所需项目的innerText。

    这里有一个简单易行的小方法

    jQuery.fn.trueText = function(obj){
        var $item = $(obj).clone();
        $item.children().remove(); 
        return $item.text();
    };
    

    现在你可以打电话了 $("#attachmentContainer").trueText()

        3
  •  2
  •   Klaster_1 Нет войне    15 年前

    $('attachmentcontainer').contents().filter(function()返回此.nodeType==3;).text())

        4
  •  1
  •   Community CDub    7 年前

    复制自 my own answer on a similar thread

    此示例使用 .contents() 要获取所有子节点(包括文本节点),请使用 .map() 将每个子节点转换为基于 nodeType . 如果节点是文本节点(即不在范围内的文本),则返回其 nodeValue .

    这将返回包含字符串的jquery集,因此我们调用 .get() 获取我们可以调用的“标准”数组对象 .join() 论。

    // our 'div' that contains your code:
    var $html = $("<div id='attachmentContainer'>\n    #Attachment#\n    <span id='spnAttachmentName' class='hidden'>#AttachmentName#</span>\n    <span id='spnAttachmentPath' class='hidden'>#AttachmentPath#</span>\n</div>");
    
    // Map the contents() into strings
    $html.contents().map(function() { 
      // if the node is a textNode, use its nodeValue, otherwise empty string
      return this.nodeType == 3 ? this.nodeValue : ''; 
      // get the array, and join it together:
    }).get().join('');
    
    // " 
    //     #Attachment# 
    //     
    //     
    // "
    

    如果要修剪多余的空白,可以使用 $.trim(this.nodeValue)

    如果你需要做很多,你甚至可以做一个插件(现在有一些选项):

    $.fn.directText = function(settings) {
       settings = $.extend({},$.fn.directText.defaults, settings);
       return this.contents().map(function() {
         if (this.nodeType != 3) return undefined; // remove non-text nodes
         var value = this.nodeValue;
         if (settings.trim) value = $.trim(value);
         if (!value.length) return undefined; // remove zero-length text nodes
         return value;
       }).get().join(settings.joinText);
    };
    
    $.fn.directText.defaults = {
       trim: true,
       joinText: ''
    };
    
        5
  •  0
  •   Assaf Lavie    15 年前

    我认为文本实际上是一个文本元素——父级的子级,所以您只需要查询第一个子级。但不确定。高温高压

        6
  •  0
  •   user229044    15 年前

    我认为正确的角度应该是把第一部分放在 <p> </p> (如果跨度不合适)。

    我以为我能得到一个过滤器来处理它,但不能完全得到它…