简体   繁体   English

如何使用javascript获取跨度内的内部文本?

[英]How to get the inner Text inside span using javascript?

The link is here; 链接在这里; http://heart.bmj.com/content/early/2016/08/10/heartjnl-2016-310003.long http://heart.bmj.com/content/early/2016/08/10/heartjnl-2016-310003.long

I have; 我有;

<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>

What have I done so far? 到目前为止,我做了什么?

I select the span.cit-title but it's innerText 我选择了span.cit-title但它是innerText

gives me; 给我;

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

I want to get only the 我只想得到

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

part. 部分。

How can I exclude span with class cit-series-title and get the raw text? 如何使用类cit-series-title排除span并获取原始文本?

The text you want is next sibling of .cit-series-title . 您想要的文本是.cit-series-title下一个兄弟。 You can select that element and use javascript Node.nextSibling to getting next sibling text of it. 您可以选择该元素,然后使用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> 

You could first get the content of the entire cit-title element... 您可以首先获取整个cit-title元素的内容...

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

...then get only the title text... ...然后只获得标题文字...

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

...and then remove the titleText from the wholeElementText ... 然后从整个wholeElementText删除titleText

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

select data from span.cit-title and store it to variable A and then select data from span.cit-series-title and store it to variable B span.cit-title选择数据并将其存储到变量A ,然后从span.cit-series-title选择数据并将其存储到变量B

then do like A.replace(B, ''); 然后像A.replace(B, ''); it will work 它会工作

 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> 

I would store the div in a temporary variable in order to remove the unwanted elements and get the text. 我会将div存储在一个临时变量中,以删除不需要的元素并获取文本。

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

I think the better solution is to wrap the other text in another span like this: 我认为更好的解决方案是将其他文本包装在另一个跨度中,如下所示:

 <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> 

And then you get the inner text from .cit-series-description instead form the container. 然后从.cit-series-description获取内部文本,而不是从容器中获取。

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM