我循环浏览我的网络应用程序中的每一个段落,并搜索一个段落中的每一个单词。大约有2000个段落需要搜索。任何改进此代码的建议将不胜感激。
function searchBarFunction(){
const wimesParagraph = document.querySelectorAll('.wimesParagraph');
let textToSearch = searchBar.value
textToSearch = textToSearch.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");
let pattern = new RegExp(`${textToSearch}`,"gi");
filterOn = 0;
let textToSearch = searchBar.value
textToSearch = textToSearch.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");
let pattern = new RegExp(`${textToSearch}`,"gi");
for(let i = 0; i < paragraph.children[0].children[4].children[0].childElementCount; i++){
paragraph.children[0].children[4].children[0].children[i].innerHTML = paragraph.children[0].children[4].children[0].children[i].textContent.replace(pattern, match => {
paragraph.style.display = 'flex';
x = 1;
if(searchBar.value === ""){
paragraph.dataset.searched = "not-searched";
} else{
paragraph.dataset.searched = "searched";
}
if(filterOn === 1){
if(paragraph.dataset.filter === "no-filters"){
paragraph.style.display = 'none';
}
}
return `<mark>${match}</mark>`;
})
}
})
}
我试图将段落放在数组中,然后搜索文本,然后更新DOM一次,因为我认为是多次更新DOM导致了延迟
1条答案
按热度按时间jhdbpxl91#
如果我是你,我的密码会是这样的