对于这个新手问题,我深表歉意,我尝试弄乱js,但由于某种原因,我无法弄清楚。我真的很感谢任何帮助。
我在博客上添加了HubSpot的默认site search框和“搜索结果”页面。它的工作方式是,当我输入搜索词(例如 dog )时,它会显示包含 dog
一词的文章的结果这部分很好,我对此没有问题,但是当我搜索狗(复数)时,它没有任何结果。相反,我收到“未找到结果”
所以它使用的是“完全匹配”而不是“广泛匹配”,我想知道是否有一种方法可以自定义它以显示狗(单数)结果,即使我搜索狗(复数)也是如此?
我已经搜索了所有COS文档,但没有发现有用的东西。
这是我的HTML + HUBL
<div class="hs-search-field">
<h3>
Search The Blog
</h3>
<div class="hs-search-field__bar">
<form id="search-form" action="/{{ site_settings.content_search_results_page_path }}">
<input type="text" class="hs-search-field__input" name="term" autocomplete="off" placeholder="{{ module.placeholder }}">
<input type="hidden" name="type" value="BLOG_POST">
<input type="hidden" name="pathPrefix" value="blog">
<button type="submit" form="search-form" value="Submit" class="hs-button primary"><i class="fa fa-search"></i></button>
<ul class="hs-search-field__suggestions"></ul>
</form>
</div>
</div>
这是默认的JS
var hsSearch = function(_instance) {
var TYPEAHEAD_LIMIT = 10;
var searchTerm = "blog_post",
searchForm = _instance,
searchField = _instance.querySelector('.hs-search-field__input'),
searchResults = _instance.querySelector('.hs-search-field__suggestions'),
searchOptions = function() {
var formParams = [];
var form = _instance.querySelector('form');
for ( var i = 0; i < form.querySelectorAll('input[type=hidden]').length; i++ ) {
var e = form.querySelectorAll('input[type=hidden]')[i];
if (e.name !== 'limit') {
formParams.push(encodeURIComponent(e.name) + "=" + encodeURIComponent(e.value));
}
}
var queryString = formParams.join("&");
return queryString;
};
var debounce = function(func, wait, immediate) {
var timeout;
return function() {
var context = this,
args = arguments;
var later = function() {
timeout = null;
if ( !immediate ) {
func.apply(context, args);
}
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait || 200);
if ( callNow ) {
func.apply(context, args);
}
};
},
emptySearchResults = function(){
searchResults.innerHTML = '<li class=\"hs-search__no-results\">No Result Found</li>';
searchField.focus();
searchForm.classList.add('hs-search-field--open');
},
fillSearchResults = function(response){
var items = [];
items.push( "<li id='results-for'>Results for \"" + response.searchTerm + "\"</li>" );
response.results.forEach(function(val, index) {
items.push( "<li id='result" + index + "'><a href='" + val.url + "'>" + val.title + "</a></li>" );
});
emptySearchResults();
searchResults.innerHTML = items.join("");
items.push( "<li>No Result </li>" );
searchForm.classList.add('hs-search-field--open');
},
getSearchResults = function() {
var request = new XMLHttpRequest();
var requestUrl = "/_hcms/search?&term="+encodeURIComponent(searchTerm)+"&limit="+encodeURIComponent(TYPEAHEAD_LIMIT)+"&autocomplete=true&analytics=true&" + searchOptions();
request.open('GET', requestUrl, true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
var data = JSON.parse(request.responseText);
if (data.total > 0) {
fillSearchResults(data);
trapFocus();
}
else {
emptySearchResults();
}
} else {
console.error('Server reached, error retrieving results.');
}
};
request.onerror = function() {
console.error('Could not reach the server.');
};
request.send();
},
trapFocus = function(){
var tabbable = [];
tabbable.push(searchField);
var tabbables = searchResults.getElementsByTagName('A');
for (var i = 0; i < tabbables.length; i++) {
tabbable.push(tabbables[i]);
}
var firstTabbable = tabbable[0],
lastTabbable = tabbable[tabbable.length-1];
var tabResult = function(e){
if (e.target == lastTabbable && !e.shiftKey) {
e.preventDefault();
firstTabbable.focus();
}
else if (e.target == firstTabbable && e.shiftKey) {
e.preventDefault();
lastTabbable.focus();
}
},
nextResult = function(e) {
e.preventDefault();
if (e.target == lastTabbable) {
firstTabbable.focus();
}
else {
tabbable.forEach(function(el){
if (el == e.target) {
tabbable[tabbable.indexOf(el) + 1].focus();
}
});
}
},
lastResult = function(e) {
e.preventDefault();
if (e.target == firstTabbable) {
lastTabbable.focus();
}
else {
tabbable.forEach(function(el){
if (el == e.target) {
tabbable[tabbable.indexOf(el) - 1].focus();
}
});
}
};
searchForm.addEventListener('keydown', function(e){
switch (e.which) {
case 9:
tabResult(e);
break;
case 27:
emptySearchResults();
break;
case 38:
lastResult(e);
break;
case 40:
nextResult(e);
break;
}
});
},
isSearchTermPresent = debounce(function() {
searchTerm = searchField.value;
if(searchTerm.length > 2) {
getSearchResults();
}
else if (searchTerm.length == 0) {
emptySearchResults();
}
}, 250),
init = (function(){
searchField.addEventListener('input', function(e) {
if ((e.which != 9) && (e.which != 40) && (e.which != 38) && (e.which != 27) && (searchTerm != searchField.value)) {
isSearchTermPresent();
}
});
})();
}
if (document.attachEvent ? document.readyState === "complete" : document.readyState !== "loading"){
var searchResults = document.querySelectorAll('.hs-search-field');
Array.prototype.forEach.call(searchResults, function(el){
var hsSearchModule = hsSearch(el);
});
} else {
document.addEventListener('DOMContentLoaded', function() {
var searchResults = document.querySelectorAll('.hs-search-field');
Array.prototype.forEach.call(searchResults, function(el){
var hsSearchModule = hsSearch(el);
});
});
}
$(document).click(function(e){
var targetbox = $('.hs-search-field');
if(!targetbox.is(e.target) && targetbox.has(e.target).length === 0){
$('.hs-search-field').removeClass("hs-search-field--open");
}
});