Jquery 自动完成 - 关注整个表,而不仅仅是
我遇到了一个问题,即使经过很多天的研究也无法解决。
我使用 Jquery 的自动完成功能,除了焦点之外,一切正常。当我专注于菜单时,整个桌子都是焦点,而不仅仅是“li”和“li”。 代码片段:
$(document).ready(function() {
$.ui.autocomplete.prototype._renderMenu = function(ul1, items) {
var self = this;
ul1.append("<table class='books'><thead><tr><th class='subtitleSearch'>BOOKS</th></thead><tbody></tbody></table>");
$.each(items, function(index, item) {
self._renderItemData(ul1, ul1.find("table.books tbody"), item);
})
ul1.append("<p class='seeMore'><a href=''>See more...</a></p>");
ul1.append("<table class='movies'><thead><tr><th class='subtitleSearch'>MOVIES</th></thead><tbody></tbody></table>");
$.each(items, function(index, item) {
self._renderItemData(ul1, ul1.find("table.movies tbody"), item);
})
ul1.append("<p class='seeMore'><a href=''>See more...</a></p>");
ul1.append("<table class='TVShows'><thead><tr><th class='subtitleSearch'>TV SHOWS</th></thead><tbody></tbody></table>");
$.each(items, function(index, item) {
self._renderItemData(ul1, ul1.find("table.TVShows tbody"), item);
})
ul1.append("<p class='seeMore'><a href=''>See more...</a></p>");
};
$.ui.autocomplete.prototype._renderItemData = function(ul, table, item) {
return this._renderItem(table, item).data("ui-autocomplete-item", item);
};
$("#input_search_form").autocomplete({
source: '/autocomplete.php',
minLength: 3,
focus: function(event, ui) {
//alert("Focus Event Triggered");
}
})
.autocomplete("instance")._renderItem = function(ul, item) {
return $("<li>")
.data("item.autocomplete", item)
.append("<tr><td rowspan='3' class='thumbTD'><img src='" + item.cover + "' alt='Cover' class='thumb' /></td><td>" + item.label + "</td></tr><tr><td class='subtitle'>" + item.author + "</td></tr><tr><td class='subtitle'>" + item.publishedDate + "</td></tr>")
.appendTo(ul);
};
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="viewport">
<div id="header" class="header">
<div class="header_top">
</div>
</div>
<div id="container">
<div class="container_search">
<div class="search">
<div class="search_box">
<span class="material-icons md-36">search</span>
<form id="search_form" class="search_form" action="autocomplete.php" method="post">
<input id="input_search_form" placeholder="Type your search here...">
</form>
</div>
</div>
</div>
</div>
</div>
演示:http://www.thebitcoinstreet.com/(不用担心网址,这是我的草稿网站)
提前谢谢您!
I got a problem that i can't resolve, even after many days of researches.
I use Autocomplete from Jquery, everything works fine except the focus. When i focus on my menu, the whole table is focus, not only 'li' by 'li'.
Snippet of code:
$(document).ready(function() {
$.ui.autocomplete.prototype._renderMenu = function(ul1, items) {
var self = this;
ul1.append("<table class='books'><thead><tr><th class='subtitleSearch'>BOOKS</th></thead><tbody></tbody></table>");
$.each(items, function(index, item) {
self._renderItemData(ul1, ul1.find("table.books tbody"), item);
})
ul1.append("<p class='seeMore'><a href=''>See more...</a></p>");
ul1.append("<table class='movies'><thead><tr><th class='subtitleSearch'>MOVIES</th></thead><tbody></tbody></table>");
$.each(items, function(index, item) {
self._renderItemData(ul1, ul1.find("table.movies tbody"), item);
})
ul1.append("<p class='seeMore'><a href=''>See more...</a></p>");
ul1.append("<table class='TVShows'><thead><tr><th class='subtitleSearch'>TV SHOWS</th></thead><tbody></tbody></table>");
$.each(items, function(index, item) {
self._renderItemData(ul1, ul1.find("table.TVShows tbody"), item);
})
ul1.append("<p class='seeMore'><a href=''>See more...</a></p>");
};
$.ui.autocomplete.prototype._renderItemData = function(ul, table, item) {
return this._renderItem(table, item).data("ui-autocomplete-item", item);
};
$("#input_search_form").autocomplete({
source: '/autocomplete.php',
minLength: 3,
focus: function(event, ui) {
//alert("Focus Event Triggered");
}
})
.autocomplete("instance")._renderItem = function(ul, item) {
return $("<li>")
.data("item.autocomplete", item)
.append("<tr><td rowspan='3' class='thumbTD'><img src='" + item.cover + "' alt='Cover' class='thumb' /></td><td>" + item.label + "</td></tr><tr><td class='subtitle'>" + item.author + "</td></tr><tr><td class='subtitle'>" + item.publishedDate + "</td></tr>")
.appendTo(ul);
};
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="viewport">
<div id="header" class="header">
<div class="header_top">
</div>
</div>
<div id="container">
<div class="container_search">
<div class="search">
<div class="search_box">
<span class="material-icons md-36">search</span>
<form id="search_form" class="search_form" action="autocomplete.php" method="post">
<input id="input_search_form" placeholder="Type your search here...">
</form>
</div>
</div>
</div>
</div>
</div>
Demo : http://www.thebitcoinstreet.com/ (don't worry about the url, this is my draft website)
Thank you in advance !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑以下示例。我已将
category
部分添加到数据中。这将创建没有表格的列表。
如果你真的想使用表格,试试这个。
添加“查看更多”类型的链接可能需要更多工作。这需要知道类别何时发生变化或在何处注入该链接。可以尝试在添加新类别项之前注入它。也可以在菜单编写完成后注入。
Consider the following example. I have added
category
section to the data.This creates the List without Tables.
If you really want to use Tables, try this.
To add an "See More" type of link may take more work. This requires knowing when the category is changing or where to inject that link. Can try to inject it before the new category item is added. Can also inject it after the menu is complied.