How to get only direct text without tags with jQuery in HTML

后端 未结 8 521
忘了有多久
忘了有多久 2020-11-27 06:15

I\'ve got an HTML:

1)TEXT THAT I ONLY NEED

some par

    ..

I need only

8条回答
  •  孤城傲影
    2020-11-27 06:44

    well, if you use jQuery, you would do

    $('.selector').text();
    

    more info and examples here

    edit: I just saw you want the text that is not in any tags - well this is not directly possible (fact is, that every text inside a html document is inside some HTML tags.)

    In this case I would try to change the markup (easiest way), so the desired text is within html tags. If this is not possible, you use the above mentioned text() method to get the whole text from the parent element, and after you have to subtract the pieces you dont want.

    see http://jsfiddle.net/3SSAR/1/ for an example of how to subtract the strings..

    just remember to use substr and not - to subtract strings in javascript

提交回复
热议问题