.
.search-
.search-
.search-input:
.search-
.search-results
.search-results a:
.no-
.no-results
无法确定要查找的内容
查找
没有找到相关内容。
const form = document.getElementById('search-form');const results = document.getElementById('search-results');
const noResults = document.getElementById('no-results');
form.addEventListener('submit', event => {
event.preventDefault();
const keyword = document.getElementById('search-input').value.trim();
const url = https://api.example.com/search?q=${encodeURIComponent(keyword)};
fetch(url)
.then(response => response.json())
.then(data => {
const resultsHtml = '';
data.results.forEach(result => {
const title = result.title;
const description = result.description;
const link = https://${result.link};
resultsHtml += ${title}${description};
});
results.innerHTML = resultsHtml;
noResults.style.display = 'block';
})
.catch(error => {
console.error(error);
noResults.style.display = 'none';
});
});