如何解决如何防止我的搜索栏需要在另一个窗口中打开
我创建了一个 JS 搜索栏。目前我的代码如下所示:
let results = document.getElementById('searchBar');
console.log(results);
let theWebsites = [];
results.addEventListener('click',(e) => {
if (window.location.href != "file:///Users/eswizzle/project/searchbarhtml.html?searchBar"){
window.location.replace("file:///Users/eswizzle/project/searchbarhtml.html?searchBar");
}
});
results.addEventListener('keypress',(e) => {
console.log(e);
if (results.value.length > 1 && e.keyCode === 13){
e.preventDefault();
const searchString = results.value.toLowerCase();
const filteredWebsites = theWebsites.filter(website => {
return website.name.toLowerCase().includes(searchString) || website.keywords.toLowerCase().includes (searchString);
});
console.log(searchString);
displayWebsites(filteredWebsites);
e.preventDefault();
results.value = "";
console.log(filteredWebsites);
}
});
const loadWebsites = async () => {
try{
const res = await fetch('http://localhost:3000/websites');
theWebsites = await res.json();
console.log(theWebsites);
} catch(err){
console.error(err);
console.log("error");
}
};
const displayWebsites = (websites) => {
console.log(websites);
let htmlString = websites.map((character) => {
return `
<li class = "normalContainerDiv">
<h2><a href = "Users/eswizzle/project${character.link}.html">${character.name}</h2></a>
</li>
`;
})
.join('');
console.log(htmlString);
if (htmlString == false){
htmlString = `
<li class = "normalContainerDiv">
<h2>No Results</h2>
</li>
`;
}
console.log(htmlString);
websiteList.innerHTML = htmlString;
waitMoment.innerHTML = "Search Results:";
};
loadWebsites();
搜索栏显示在每个页面的顶部。在第 4 行,我必须为单击创建一个事件侦听器。当用户点击搜索栏时,如果用户不在搜索结果页面上,网站会自动重定向到该页面。我必须创建此代码,因为如果没有此代码,则无法从搜索结果页面之外的其他页面进行搜索,因为不会显示任何结果。
如何允许用户不仅从搜索结果页面而且从其他页面进行搜索?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。