코드 예제를 사용하여 here을 찾았으며 사용자가 콤보 상자의 옵션 위에 마우스를 가져갈 때 메시지가 포함 된 툴팁을 만드는 함수를 추가하려고합니다 . 툴팁은 모든 옵션에 대해 호버 위에 표시되어야합니다.jQuery ComboBox - 목록의 옵션에 대한 이벤트 (마우스 입력/마우스 오버)
이 단계에서 이벤트를 시작하고 싶습니다. 메시지 자체는 나중에 정의 할 내용이 될 수 있습니다. 누군가 제발 도와 줄 수 있니? jQuery를 절차는 원래의 목록을 숨 깁니다 것을 명심하는 것이 중요하다
$(function() {
$.widget("custom.combobox", {
_create: function() {
this.wrapper = $("<span>")
.addClass("custom-combobox")
.insertAfter(this.element);
this.element.hide();
this._createAutocomplete();
this._createShowAllButton();
this._hoverOption();
},
\t
\t
/*
*
* Hover Event
*
*/
_hoverOption: function() {},
\t
_createAutocomplete: function() {
var selected = this.element.children(":selected"),
value = selected.val() ? selected.text() : "";
this.input = $("<input>")
.appendTo(this.wrapper)
.val(value)
.attr("title", "")
.addClass("custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left")
.autocomplete({
delay: 0,
minLength: 0,
source: $.proxy(this, "_source")
})
.tooltip({
classes: {
"ui-tooltip": "ui-state-highlight"
}
});
this._on(this.input, {
autocompleteselect: function(event, ui) {
ui.item.option.selected = true;
this._trigger("select", event, {
item: ui.item.option
});
},
autocompletechange: "_removeIfInvalid"
});
},
_createShowAllButton: function() {
var input = this.input,
wasOpen = false;
$("<a>")
.attr("tabIndex", -1)
.attr("title", "Show All Items")
.tooltip()
.appendTo(this.wrapper)
.button({
icons: {
primary: "ui-icon-triangle-1-s"
},
text: false
})
.removeClass("ui-corner-all")
.addClass("custom-combobox-toggle ui-corner-right")
.on("mousedown", function() {
wasOpen = input.autocomplete("widget").is(":visible");
})
.on("click", function() {
input.trigger("focus");
// Close if already visible
if (wasOpen) {
return;
}
// Pass empty string as value to search for, displaying all results
input.autocomplete("search", "");
});
},
_source: function(request, response) {
var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
\t \t var result = [];
result = this.element.children("option").map(function() {
var text = $(this).text();
if (this.value && (!request.term || matcher.test(text)))
return {
label: text,
value: text,
option: this
};
});
\t \t if(result.length == 1 && request.term.length > this.options.previousValue.length){
\t \t \t \t result[0].option.selected = true;
\t \t \t \t this._trigger("select", null, {
\t \t \t \t \t item: result[0].option
\t \t \t \t });
\t \t \t \t this.input.val(result[0].label);
\t \t \t \t this.options.previousValue = result[0].label;
\t \t }else{
\t \t \t this.options.previousValue = request.term; \t \t
\t \t }
\t \t response(result);
\t \t
},
_removeIfInvalid: function(event, ui) {
// Selected an item, nothing to do
if (ui.item) {
return;
}
// Search for a match (case-insensitive)
var value = this.input.val(),
valueLowerCase = value.toLowerCase(),
valid = false;
this.element.children("option").each(function() {
if ($(this).text().toLowerCase() === valueLowerCase) {
this.selected = valid = true;
return false;
}
});
// Found a match, nothing to do
if (valid) {
return;
}
},
_destroy: function() {
this.wrapper.remove();
this.element.show();
}
});
$("#combobox").combobox();
});
li.ui-menu-item {
\t padding-left: 15px; \t
}
.custom-combobox {
\t position: relative;
\t display: inline-block;
\t margin-right: 40px;
}
.custom-combobox-toggle {
position: absolute;
top: 0;
bottom: 0;
margin-left: -1px;
padding: 0;
}
.custom-combobox-input {
margin: 0;
padding: 5px 10px;
}
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
<select id="combobox">
<option value="">Select one...</option>
<option value="good">one</option>
<option value="bad">two</option>
<option value="ugly">three</option>
</select>
이 문제를 해결 :
여기 내 코드입니다. ul li 구조로 새 목록을 만듭니다.
이 때문에 사용자가 대신 jQuery를 사용하여 사용자가 원래 목록과 상호 작용할 수 없기 때문에 옵션 목록에 대한 이벤트를 만들면 호버에는 사용할 수 없습니다.
사용자가 옵션을 선택하면 jQuery가 원래 옵션 목록을 업데이트합니다.
따라서 mouseover/hover/mouseenter 이벤트가 여기에서 작동하려면 jQuery에서 만든 목록에 바인드해야합니다.
나는 두려워하지 않는다. –
@JacquesJoubert 옵션 위에 마우스를 올려 놓을 때 실행해야합니다 (옵션을 클릭하지 마십시오) – ScanQR
@JacquesJoubert 업데이트 된 답변으로 시도하지 마십시오. – ScanQR