Get only visible text with jquery

前端 未结 3 1870
死守一世寂寞
死守一世寂寞 2021-01-13 16:53

Lets say I have some html like this:

FoohiddenBar

In

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-13 17:46

    create a clone, add it to the DOM (as pointed out by slindberg), remove all hidden elements, and then get the text :

    var clone = $('#content').clone();
    
    clone.appendTo('body').find(':hidden').remove();
    
    var text = clone.text();
    
    clone.remove();
    

    FIDDLE

提交回复
热议问题