Один из возможных способов:
// using a named function, passing in a CSS selector:
let getAllAfter = function(selector) {
// here we return the result of document.querySelectorAll(),
// after using Array.from() to convert the Array-like NodeList
// to an Array:
return Array.from(
document.querySelectorAll(
// using the passed-in selector and concatenating that
// with the general sibling combinator ('~') and, in this
// case using '.letters' to restrict the selector to elements
// containing that class:
selector + '~ .letters')
);
}
// setting the textContent of the element with the id of 'output'
// to be equal to the Array of elements - having mapped
// their textContent using Array.prototype.map(), and joining
// the resulting Array using Array.prototype.join():
document.getElementById('output').textContent = getAllAfter('#c').map(el => el.textContent).join(', ');
#output {
color: red;
}
<h1 id='a' class='letters'>A</h1>
<h1 id='b' class='letters'>B</h1>
<h1 id='c' class='letters'>C</h1>
<h1 id='d' class='letters'>D</h1>
<h1 id='e' class='letters'>E</h1>
<h1 id='f' class='letters'>F</h1>
<h1 id='output'></h1>
Это, конечно, использование жестко закодированного селектора, который не особенно полезен в различных контекстах, поэтому измените вышеупомянутую функцию, чтобы принятьвторой селектор, чтобы ограничить выбранные последующие братья и сестры селектором:
// using a named function, passing in a CSS selector:
let getAllAfter = function(targetSelector, siblingSelector) {
// here we return the result of document.querySelectorAll(),
// after using Array.from() to convert the Array-like NodeList
// to an Array:
return Array.from(
document.querySelectorAll(
// using the passed-in selector and concatenating that
// with the general sibling combinator ('~') and, in this
// case using '.letters' to restrict the selector to elements
// containing that class:
targetSelector + ' ~ ' + siblingSelector)
);
}
// setting the textContent of the element with the id of 'output'
// to be equal to the Array of elements - having mapped
// their textContent using Array.prototype.map(), and joining
// the resulting Array using Array.prototype.join():
document.getElementById('output').textContent = getAllAfter('#c', '.letters').map(el => el.textContent).join(', ');
#output {
color: red;
}
<h1 id='a' class='letters'>A</h1>
<h1 id='b' class='letters'>B</h1>
<h1 id='c' class='letters'>C</h1>
<h1 id='d' class='letters'>D</h1>
<h1 id='e' class='letters'>E</h1>
<h1 id='f' class='letters'>F</h1>
<h1 id='output'></h1>
Конечно, в совместимых браузерах мы могли бы избежать объединения строк и просто использовать шаблонный литерал для преобразования предоставленных строк в селектор:
// using a named function, passing in a CSS selector:
let getAllAfter = function(targetSelector, siblingSelector) {
// here we return the result of document.querySelectorAll(),
// after using Array.from() to convert the Array-like NodeList
// to an Array:
return Array.from(
document.querySelectorAll(
// using the passed-in selector and concatenating that
// with the general sibling combinator ('~') and, in this
// case using '.letters' to restrict the selector to elements
// containing that class:
`${targetSelector} ~ ${siblingSelector}`)
);
}
// setting the textContent of the element with the id of 'output'
// to be equal to the Array of elements - having mapped
// their textContent using Array.prototype.map(), and joining
// the resulting Array using Array.prototype.join():
document.getElementById('output').textContent = getAllAfter('#c', '.letters').map(el => el.textContent).join(', ');
#output {
color: red;
}
<h1 id='a' class='letters'>A</h1>
<h1 id='b' class='letters'>B</h1>
<h1 id='c' class='letters'>C</h1>
<h1 id='d' class='letters'>D</h1>
<h1 id='e' class='letters'>E</h1>
<h1 id='f' class='letters'>F</h1>
<h1 id='output'></h1>
Список литературы: