javascript - Incorrect work of jQuery function .text() -


i'm trying text html without tags using $('.container').text(). instead of getting clear text text duplicate parts.

here html code:

<div class="v highlighted" id="4">     <span class="vn" id="4">4</span>     <span class="p">text-0</span><br>     <span class="p">         <span class="wj">             text-1             <span class="w">text-2</span>             text-3         </span>     </span><br> </div>  <script>     console.log($(".highlighted :not(.vn)").text()); </script> 

in console see result:

text-0text-1text-2text-3text-1text-2text-3text-2 

does know why happens?

look @ .highlighted :not(.vn) matches.

it matches:

  • <span class="p">text-0</span>
  • <br>
  • <span class="p"><span class="wj">text-1<span class="w">text-2</span>text-3</span></span>
  • <span class="wj">text-1<span class="w">text-2</span>text-3</span>
  • <span class="w">text-2</span>
  • <br>

since have text contained in span contained in span , both spans match selector, content of outer span and (identical) content of inner span.

you want use child combinator (>) instead of descendant combinator () in selector.

.highlighted > :not(.vn) 

Comments