[英]Getting text from inside P element, not including span tag in selection
How do I remove the span element from the selection in order to get just the text in the P tag, and not include the span tag or its contents? 如何从选择中删除span元素,以便仅获取P标记中的文本,而不包括span标记或其内容?
<p><span class="title visible-xs-inline">Description: </span>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
I have tried using .not(), but that doesn't work in this case. 我尝试使用.not(),但是在这种情况下不起作用。
var theValue = $(this).parents().find('p').not('.title').text();
Due to the HTML structure you would need to retrieve the textNode itself. 由于HTML结构,您将需要检索textNode本身。 Try this:
尝试这个:
var text = $('p').contents().last()[0].nodeValue;
This answer provides an excellent example. 这个答案提供了一个很好的例子。 Just clone the element in memory and remove its children, then call
.text()
. 只需在内存中克隆元素并删除其子元素,然后调用
.text()
。
$('button').click(function() { var text = $("p") .clone() //clone the element .children() //select all the children .remove() //remove all the children .end() //again go back to selected element .text(); alert(text); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p><span class="title visible-xs-inline">Description: </span>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> <button>Get P Text Not Including Span</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.