簡體   English   中英

從P元素內部獲取文本,選擇中不包含span標簽

[英]Getting text from inside P element, not including span tag in selection

如何從選擇中刪除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();

由於HTML結構,您將需要檢索textNode本身。 嘗試這個:

var text = $('p').contents().last()[0].nodeValue;

小提琴的例子

這個答案提供了一個很好的例子。 只需在內存中克隆元素並刪除其子元素,然后調用.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM