Эта функция будет возвращать innerText
из h2
внутри второго header
:
function() {
var headerElement = document.getElementsByTagName('header')[1];
var h2Element = headerElement.getElementsByTagName('h2')[0];
var innerText = h2Element.innerText;
console.log('The text you need is: ' + innerText);
return innerText;
}
Кроме того, предполагая, что ваш HTML-код НЕ будет иметь никаких других h2
элемент, и вы всегда хотите получить тот же элемент h2
, который вы можете попробовать:
function() {
var h2Element = document.getElementsByTagName('h2')[1];
var innerText = h2Element.innerText;
console.log('The text you need is: ' + innerText);
return innerText;
}