這是參考代碼在這個小提琴:http://jsfiddle.net/x8whpx09/17/停止事件的內容時,點擊搜索結果中的div
我試圖創建在用戶輸入的東西,會彈出一個結果列表的搜索框。我遇到的問題是當用戶點擊除搜索結果列表將關閉的輸入元素以外的任何地方。我有一個附加到父DIV的焦點事件,但即使我將它附加到輸入,我也會得到相同的行爲。我想要的是用戶能夠選擇列表上的滾動條,而不是讓列表消失。如果他們點擊輸入的外部或列表,那麼列表應該關閉。
現在它關閉,無論他們點擊哪裏,除非它在輸入中。
編輯:performSearch函數體包裝在AJAX請求中。
SOLUTION:我終於明白髮生了什麼事。您需要綁定到mousedown事件並返回false以允許在下拉列表中選擇滾動條。這question終於給了我解決方案。
$target.on('mousedown',function (event) {
return false;
});
這裏是在小提琴代碼:
var search = {
closing: null,
performSearch: function(elem, e) {
var $this = $(elem);
var $target = $('#search-results');
if ($target.length === 0) {
$target = $('<div id="search-results" class="sb-list"></div>');
$target.insertAfter($this);
var p = $this.position();
$target.css('top', (p.top + $this.height() + 15) + 'px');
$target.css('width', ($this.width() + p.left + 10) + 'px');
$target.css('width', ($this.width() + 25) + 'px');
$target.css('left', (p.left) + 'px');
$target.slideDown('fast', function() {
$target.css('overflow', 'auto');
});
$target.on('mouseover', '.sb-row', function() {
$(elem).addClass('sb-active');
$('.sb-active').removeClass('sb-active');
});
$target.on('click', '.sb-row', search.click);
//$target.parent().on('focusin focusout', search.listFocus);
} else {
$target.empty();
$target.slideDown('fast', function() {
$target.css('overflow', 'auto');
});
}
for (i = 0; i < 40; i++) {
var html = [];
html.push('<div class="sb-row' + (i == 0 ? ' sb-active' : '') + '">');
html.push('<a href="#">test result' + i + '</a>');
html.push('</div>');
var $out = $(html.join(''));
$target.append($out);
}
},
delayClose: function(e) {
console.log('delayClose');
var self = this;
console.log('results focused: ' + $('#search-results').is(':focus'));
console.log('div focused: ' + $('#parent').is(':focus'));
search.closing = window.setTimeout(function() {
search.close.apply(self, arguments);
}, 1000);
},
listFocus: function(e) {
console.log(e.type);
var self = this;
window.clearTimeout(search.closing);
},
click: function(e) {
console.log('clicked');
},
close: function(e) {
console.log('closing');
window.clearTimeout(search.closing);
var $this = $(this);
$('#search-results').slideUp('fast');
}
};
$(document).ready(function() {
$searchBox = $('#search');
$searchBox.on('focus', function(e) {
this.value = '';
this.select();
e.stopPropagation();
return false;
});
$searchBox.on('keyup', function(e) {
search.performSearch($searchBox[0], e);
});
$searchBox.parent().on('focusout', search.delayClose);
});
.sb-list {
position: absolute;
width: 250px;
top: 0;
left: 0;
height: 300px;
background: #FAFAFA;
border: solid 1px #999;
border-top: none;
display: none;
overflow: auto;
z-index: 92592;
-webkit-box-shadow: 0 2px 2px #999999;
-moz-box-shadow: 0 2px 2px #999999;
-ms-box-shadow: 0 2px 2px #999999;
box-shadow: 0 2px 2px #999999;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="parent">
<input type="text" id="search" />
</div>
[mcve](http://stackoverflow.com/help/mcve)怎麼樣? – Teemu 2015-02-11 20:55:56
你提供的JSFiddle片段沒有這個問題(下拉不會消失),但也不會自動關閉。您是否在尋找能夠在外部點擊時關閉下拉菜單的功能?還是一個會在點擊下拉菜單時保持輸入焦點? – 2015-02-11 21:07:56
@JánosWeisz我希望當你點擊它的「外部」或輸入時關閉下拉菜單,但不要點擊內部或輸入內容。 – coryrwest 2015-02-11 21:13:17