如何在没有特定子元素的情况下获取div中的内容

Mus*_*Tek 4 html javascript css jquery phantomjs

<div class="news"> 
    i need this content

   <figure class="summary">
     i dont need this content
    </figure>

 </div>
Run Code Online (Sandbox Code Playgroud)

我想在课堂上得到“我需要这个内容” .news,但在课堂上没有“我不需要这个内容”.summary

$("div.news").not("figure.summary").text()
Run Code Online (Sandbox Code Playgroud)

我尝试了这个jquery,但仍然在图形标签中获取文本。

A. *_*lff 5

alert($("div.news").contents().not("figure.summary").text());
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="news"> 
    i need this content

   <figure class="summary">
     i dont need this content
    </figure>

 </div>
Run Code Online (Sandbox Code Playgroud)

您应该使用contents()包含文本节点:

$("div.news").contents().not("figure.summary").text()
Run Code Online (Sandbox Code Playgroud)

如果你只想获取文本节点,而不是按特定的类、类型等过滤掉......:

$("div.news").contents().not("div.news *").text()
Run Code Online (Sandbox Code Playgroud)

了解更多信息.contents(): http: //api.jquery.com/contents/