如何从HTML代码中<strong>标记后检索数据?

时间:2016-05-31 12:25:46

标签: javascript html

我需要从HTML代码中检索部分数据。这是:

<article class="article-detail-description">
<h1 class="page-heading">
Przedterminowe wybory parlamentarne w Europie Środkowej i Wschodniej. Model normatywny, przyczyny i konsekwencje
<br /><small>Early parliamentary elections in Central and Eastern Europe. Normative model, reasons and consequences</small>
</h1>

<div>
<strong>Author(s): </strong>Andrzej Antoszewski<br />
<strong>Subject(s): </strong>Politics / Political Sciences<br />
<strong>Published by: </strong>Łódzkie Towarzystwo Naukowe<br/>
<strong>Keywords: </strong>East-Central Europe; early election; parliament; normative model<br/>
</div>

我可以从article-additional-info类中获取上述代码所属的所有信息。我可以使用document.getElementsByClassName("article-additional-info")[0].innerText

来完成

如何获取个人信息,例如:Author(s):? 我想避免使用RegEx。

1 个答案:

答案 0 :(得分:1)

你可以做这样的事情

// app/config/routes.js
module.exports.routes = {
 ...
'get /user'     : 'UserController.form',
'post /user'    : 'UserController.add',
...
}
// get all span ccontent
Array.from(document.getElementsByClassName('article-additional-info')[0].getElementsByTagName('strong')).forEach(function(e) {
  console.log(e.textContent);
});

// get all elements including text node
Array.from(document.getElementsByClassName('article-additional-info')[0].getElementsByTagName('div')[0].childNodes).forEach(function(e) {
  e.textContent.trim() && console.log(e.textContent.trim());
});

// or Array.from(document.querySelector('.article-additional-info div').childNodes)