如何从选择中删除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>
我尝试过使用.not(),但在这种情况下不起作用。
var theValue = $(this).parents().find('p').not('.title').text();
答案 0 :(得分:0)
答案 1 :(得分:0)
This回答提供了一个很好的例子。只需克隆内存中的元素并删除其子元素,然后调用.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);
});
&#13;
<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>
&#13;