Код: Выделить всё
function ShowSearchResults() {
FindSearchResults();
resultListContainer.style.display = 'block';
setTimeout(() => {
resultListContainer.style.transitionDuration = '300ms'
resultListContainer.style.opacity = '1';
}, 1);
}
function HideSearchResults() {
resultListContainer.style.transitionDuration = '500ms'
resultListContainer.style.opacity = '0';
setTimeout(() => {
resultListContainer.style.display = 'block';
}, 500);
}
function FindSearchResults(){
let result = [];
let input = searchInput.value;
if (input.length > 0){
result = searchTerms.filter((keyword) => {
return keyword.toLowerCase().includes(input.toLowerCase());
});
}
else{
result = searchTerms
}
DisplaySearchResults(result);
}
function DisplaySearchResults(result) {
const content = result.map((list) => {
return "[*]" + list + "";
});
resultList.innerHTML = "[list]" + content.join('') + "[/list]";
document.querySelectorAll('.result-list li').forEach((item) => {
item.addEventListener('click', function () {
const searchText = item.textContent;
highlightMatches(searchText);
scrollToFirstMatch(searchText);
});
});
}
function highlightMatches(searchText) {
clearHighlights();
const regex = new RegExp(searchText, 'gi');
const highlightedText = bodyText.replace(regex, (match) => {
return `${match}`;
});
document.body.innerHTML = highlightedText;
searchInput.focus();
}
function scrollToFirstMatch(searchText) {
const firstMatch = document.querySelector('.highlight-span');
if (firstMatch) {
firstMatch.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
}
function clearHighlights() {
document.body.innerHTML = document.body.innerHTML.replace(/(.*?)/g, '$1');
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... ch-results
Мобильная версия