2016-09-28 107 views
0

鏈接在這裏; http://heart.bmj.com/content/early/2016/08/10/heartjnl-2016-310003.long如何使用javascript獲取span內部的文本內容?

我有;

<span class="cit-title"> 
    <span class="cit-series-title">Original article 
     <span class="cit-sep cit-sep-after-article-series-title">:</span> 
    </span>Pregnancy outcomes in patients with pulmonary arterial hypertension associated with congenital heart disease 
</span> 

到目前爲止我做了什麼?

我選擇span.cit-title但它innerText

給我;

"Original article: Pregnancy outcomes in patients with pulmonary arterial hypertension associated with congenital heart disease" 

我想只有

"Pregnancy outcomes in patients with pulmonary arterial hypertension associated with congenital heart disease"` 

一部分。

如何排除類別爲cit-series-title的跨度並獲取原始文本?

+3

這是http://stackoverflow.com/questions/6520192/get-text-node-of-an-element的副本。你會在那裏找到你的答案。 – Max

+0

是否真的需要很多跨度? – vamsi

+0

我嘗試解析html。所以我不是跨度的作者。 :) – user6468132

回答

-1

我認爲更好的解決辦法是包裝在另一個這樣跨度的其他文字:

<span class="cit-title"> 
 
    <span class="cit-series-title">Original article<span class="cit-sep cit-sep-after-article-series-title">:</span> </span><span class="cit-series-description">Pregnancy outcomes in patients with pulmonary arterial hypertension associated with congenital heart disease</span> </span>

然後你從.cit-series-description內部文本,而不是形成容器。

乾杯

0

你想要的文字是.cit-series-title下一個兄弟。您可以選擇該元素並使用javascript Node.nextSibling獲取它的下一個兄弟文本。

var text = $(".cit-title > .cit-series-title")[0].nextSibling.textContent; 
 
console.log(text);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<span class="cit-title"> 
 
    <span class="cit-series-title">Original article 
 
     <span class="cit-sep cit-sep-after-article-series-title">:</span> 
 
    </span>Pregnancy outcomes in patients with pulmonary arterial hypertension associated with congenital heart disease 
 
</span>

0

你可以先把整個cit-title元素的內容...

var wholeElementText = $('.cit-title').text(); 

...那麼得到的只是標題文字...

var titleText = $('.cit-title .cit-series-title').text(); 

...和然後wholeElementText

var justThePartYouWant = wholeElementText.replace(titleText, ''); 
0

選擇數據從span.cit-title刪除titleText並將其存儲到變量A,然後從span.cit-series-title選擇數據並將其存儲到變量B

那麼不喜歡A.replace(B, '');它會工作

0

alert($(".cit-title").clone()  
 
      .children() 
 
      .remove() 
 
      .end()  
 
      .text());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<span class="cit-title"> 
 
    <span class="cit-series-title">Original article 
 
     <span class="cit-sep cit-sep-after-article-series-title">:</span> 
 
    </span>Pregnancy outcomes in patients with pulmonary arterial hypertension associated with congenital heart disease 
 
</span>

0

我會將div存儲在臨時變量中以刪除不需要的元素並獲取文本。

var div = $(".cit-title").clone(); 
div.find("span").first().remove(); 
div.text(); 
相關問題