fix: 修复失焦和点击事件冲突问题

This commit is contained in:
woomay
2020-04-06 12:14:41 +08:00
parent 6c5e6f8221
commit e439545d4b

View File

@@ -47,8 +47,7 @@ $(function () {
var EVENT_SEARCH = 'search'; var EVENT_SEARCH = 'search';
// 关键词搜索输入 // 关键词搜索输入
$('#search_keyword').on('keyup', function (event) { $('#search_keyword').on('keyup', function (event) {
var $keyword = $(this); var keyword = $(this).val();
var keyword = $keyword.val();
if(event.which==13){ if(event.which==13){
if($('#search_result .active').length>0){ if($('#search_result .active').length>0){
keyword = $('#search_result .active').eq(0).text(); keyword = $('#search_result .active').eq(0).text();
@@ -61,8 +60,18 @@ $(function () {
if(bl){ if(bl){
keywordChange(keyword); keywordChange(keyword);
} }
}).on('blur', function () { }).on('blur', function () {
// 推荐结果跳转
$('#search_result').on('click', 'li', function () {
var word = $(this).text();
$('#search_keyword').val(word);
openSearch(word);
$('#search_result').hide(); $('#search_result').hide();
});
// 解决失焦和点击事件冲突问题
setTimeout(function() {
$('#search_result').hide();
}, 100)
}).on('focus', function () { }).on('focus', function () {
var keyword = $(this).val(); var keyword = $(this).val();
keywordChange(keyword); keywordChange(keyword);
@@ -105,7 +114,8 @@ $(function () {
} }
} }
search_result.find('.active').removeClass('active'); search_result.find('.active').removeClass('active');
search_result.find('.result-item').eq(hove_li).addClass('active'); search_result.find('.result-item').eq(hove_li).addClass('active');
$('#search_keyword').val(search_result.find('.result-item').eq(hove_li).addClass('active').text());
} }
function keywordChange(keyword) { function keywordChange(keyword) {
@@ -148,15 +158,6 @@ $(function () {
} }
}); });
// 推荐结果跳转
$('#search_result').on('click', 'li', function () {
var word = $(this).text();
$('#search_keyword').val(word);
openSearch(word);
$('#search_result').hide();
});
$(document).on(EVENT_CLEAR_KEYWORD, function () { $(document).on(EVENT_CLEAR_KEYWORD, function () {
$('#clear_keyword').hide(); $('#clear_keyword').hide();
$('#search_result').hide(); $('#search_result').hide();