gpt4 book ai didi

javascript - 我怎样才能将此 addEventListener 更改为单击按钮

转载 作者:行者123 更新时间:2023-11-30 19:01:30 25 4
gpt4 key购买 nike

所以我有这个搜索 json javascript,它将结果与您键入的内容相匹配,并且它会自动执行。这些行就是这样做的(如下)。有什么方法可以将其更改为仅在按下按钮时起作用?

const searchInput = document.querySelector('.search');
const suggestions = document.querySelector('.suggestions');

searchInput.addEventListener('change', displayMatches);
searchInput.addEventListener('keyup', displayMatches);

这是函数 -

 function displayMatches() {
const matchArray = findMatches(this.value, name);
const html = matchArray.map(place => {
const regex = new RegExp(this.value);
const nameName = place.name.replace(regex, `<span class="hl">${this.value}</span>`);
return `
<a href="${place.url}" target="_blank">
<li>
<span class="name">${nameName} <br> ${(place.price)}</span>
<img src="${place.imgurl}" alt="Drink Image" height="87.5" width="100">
</li>
</a>
`;
}).join('');
suggestions.innerHTML = html;
}

以下所有当前代码:

const endpoint = "https://gist.githubusercontent.com/valeriu7474/4df04fafd994c2f778847a3e94451b44/raw/d288ddbc9cbc8bbcf89a10f2a8ead9eecb4962f6/allcurrentshops";

const name = [];
fetch(endpoint).then(blob => blob.json())
.then(data => name.push(...data));

function findMatches(wordToMatch, name) {
return name.filter(place => {
//we need to figure out if the name match
const regEx = new RegExp(wordToMatch, 'gi');
return place.name.match(regEx);
});
}

// function displayMatches() {
// const matchArray = findMatches(this.value, name);
// const html = matchArray.map(place => {
// const regex = new RegExp(this.value);
// const nameName = place.name.replace(regex, `<span class="hl">${this.value}</span>`);
// return `
// <a href="${place.url}" target="_blank">
// <li>
// <span class="name">${nameName} <br> ${(place.price)}</span>
// <img src="${place.imgurl}" alt="Drink Image" height="87.5" width="100">
// </li>
// </a>
// `;
// }).join('');
// suggestions.innerHTML = html;
// }

function displayMatches() {
const searchText = document.querySelector('.search');
const matchArray = findMatches(searchText, name);
const html = matchArray.map(place => {
const regex = new RegExp(searchText);
const nameName = place.name.replace(regex, <span class="hl">${searchText}</span>);
return ` <a href="${place.url}" target="_blank"> <li> <span class="name">${nameName} <br> ${(place.price)}</span> <img src="${place.imgurl}" alt="Drink Image" height="87.5" width="100"> </li> </a> `; }).join(''); suggestions.innerHTML = html;
}

// const searchInput = document.querySelector('.search');
// const suggestions = document.querySelector('.suggestions');

const searchBtn = document.querySelector('.btn-search');
searchBtn.addEventListener('click', displayMatches);

最佳答案

.search 字段中删除事件监听器,并添加一个按钮。

const searchBtn = document.querySelector('.searchBtn');
searchBtn.addEventListener('click', displayMatches);

编辑:
如果我正确解释该函数,请使用输入的搜索文本的新变量更新 displayMatches

function displayMatches() {
const searchText = document.querySelector('.search').value;
const matchArray = findMatches(searchText, name);
const html = matchArray.map(place => {
const regex = new RegExp(searchText);
const nameName = place.name.replace(regex, <span class="hl">${searchText}</span>);
return ` <a href="${place.url}" target="_blank"> <li> <span class="name">${nameName} <br> ${(place.price)}</span> <img src="${place.imgurl}" alt="Drink Image" height="87.5" width="100"> </li> </a> `;
}).join('');
suggestions.innerHTML = html;
}

关于javascript - 我怎样才能将此 addEventListener 更改为单击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59471134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com