[英]get text except a particular class or id in jquery
I want to extract text from html div.我想从 html div 中提取文本。
<div id="example">
I am posting a <span class='outer'>a new question <span class='inner'>in stackoverflow</span></span>
</div>
const text = $("#example")
.clone()
.children()
.removeClass("inner")
.end()
.text();
In text
i am getting I am posting a new question in stackoverflow
.在
text
我I am posting a new question in stackoverflow
。 but i want output would be I am posting a new question
.但我想要的输出是
I am posting a new question
。 I don't want the inner span.我不想要内部跨度。 Anyone can help me here.
任何人都可以在这里帮助我。 thanks:)
谢谢:)
You were close.你很接近。 Just
find
and remove
that element只需
find
并remove
该元素
const text = $("#example") .clone() .find('.inner') .remove() .end() .text(); console.log(text)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="example"> I am posting a <span class='outer'>a new question <span class='inner'>in stackoverflow</span></span> </div>
Try this:试试这个:
let clone = $("#example").clone();
clone.find('.inner').remove();
let text = clone.text();
In your example, you're just removing the class from the element, not the element itself.在您的示例中,您只是从元素中删除类,而不是元素本身。
You need to remove the element, not remove a class.您需要删除元素,而不是删除类。
const text = $("#example") .clone() .find(".inner") .remove() .end() .text(); console.log(text);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="example"> I am posting a <span class='outer'>a new question <span class='inner'>in stackoverflow</span></span> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.