Getting text within element excluding decendants

后端 未结 2 1276
别那么骄傲
别那么骄傲 2020-11-28 15:09

I see that jQuery has the .text() function for getting all text within an element and its descendant elements.

Is there a way to only get the text that

2条回答
  •  攒了一身酷
    2020-11-28 15:52

    Working example at jsFiddle

    var divClone = $('div').clone();    //  WARNING:
    divClone.children().remove();       //  This code is much slower than the 
    alert(divClone.text());             //  code in the selected answer.
    

    Update

    The performance test in @patrick dw's answer clearly shows that his approach is the better alternative. I did not delete this answer, because I think it is helpful to future readers of the question who might not know which way is better.

提交回复
热议问题