How to get the text of a div which is not a part of any other container in JQuery?

后端 未结 6 1640
野趣味
野趣味 2021-02-05 15:10

This should be real easy. Given below is the HTML.

#Attachment#
6条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-05 15:42

    This will get you just that items text

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

    clone the object so you don't have to remove the actual items children. Then you can remove the child elements and that will leave the innerText of the item you want.

    And here's a handy little method to do this easily

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

    Now you can call $("#attachmentContainer").trueText()

提交回复
热议问题