我怀疑您缺少 div 包装器。
工作示例:http://jsfiddle.net/Twisty/bZBLf/31/
jQuery
$(function() {
$("#txtBookSearch").autocomplete({
source: function(request, response) {
var booksUrl = "https://www.googleapis.com/books/v1/volumes?printType=books&maxResults=20&q=" + encodeURIComponent(request.term);
$.ajax({
url: booksUrl,
dataType: "jsonp",
success: function(data) {
response($.map(data.items, function(item) {
console.log(item);
if (item.volumeInfo.authors && item.volumeInfo.title && item.volumeInfo.industryIdentifiers && item.volumeInfo.publishedDate) {
return {
// label value will be shown in the suggestions
ebook: (item.saleInfo.isEbook == null ? "" : item.saleInfo.isEbook),
title: item.volumeInfo.title,
id: item.id,
author: item.volumeInfo.authors[0],
authors: item.volumeInfo.authors,
isbn: item.volumeInfo.industryIdentifiers,
publishedDate: item.volumeInfo.publishedDate,
image: (item.volumeInfo.imageLinks == null ? "" : item.volumeInfo.imageLinks.thumbnail),
small_image: (item.volumeInfo.imageLinks == null ? "" : item.volumeInfo.imageLinks.smallThumbnail),
description: item.volumeInfo.description
};
}
}));
}
});
},
select: function(event, ui) {
$('#divDescription').empty();
if (ui.item.image != '') {
$('#divDescription').append('<img src="' + ui.item.image + '" style="float: left; padding: 10px;">');
}
if (ui.item.ebook == true) {
$('#divDescription').append('<h2>(Ebook version)</h2>');
}
$('#divDescription').append('<p><b>Title:</b> ' + ui.item.title + '</p>');
$('#divDescription').append('<p><b>Authors:</b> ' + ui.item.authors.join(', ') + '</p>');
$('#divDescription').append('<p><b>First published year:</b> ' + ui.item.publishedDate + '</p>');
// and the usual description of the book
$('#divDescription').append('<p><b>Description:</b> ' + ui.item.description + '</p>');
if (ui.item.isbn && ui.item.isbn[0].identifier) {
$('#divDescription').append('<p><b>ISBN:</b> ' + ui.item.isbn[0].identifier + '</p>');
$('#divDescription').append('<a href="http://www.worldcat.org/isbn/' + ui.item.isbn[0].identifier + '" target="_blank">View item on worldcat</a>');
$('#divDescription').append('<p>Some users may own this book in a different edition, <a href="http://books.google.com/books?q=editions:ISBN' + ui.item.isbn[0].identifier + '&id=' + ui.item.id + '" target="_blank">check out other versions on google</a> and search their ISBN here</p>');
}
},
minLength: 2,
focus: function(event, ui) {
event.preventDefault();
},
})
.autocomplete('instance')._renderItem = function(ul, item) {
var img = $('<image class="imageClass" src=' + item.small_image + ' alt= n/a' + '/>');
var link = $('<a>' + item.title + ', ' + item.author + ', ' + item.publishedDate + (item.ebook == "" ? "" : ', (Ebook version)') + '</a>');
return $('<li>')
.append("<div>" + img.prop("outerHTML") + link.prop("outerHTML") + "</div>")
.appendTo(ul);
}
$("#txtBookSearch").on("autocompleteselect", function(e, ui) {
e.preventDefault();
});
$("txtBookSearch").keydown(function(event) {
if (event.keyCode == 13) {
if ($("txtBookSearch").val().length == 0) {
event.preventDefault();
return false;
}
}
});
});
当您创建图像和链接时,它们应该包含在div 中。这显示在以下示例中:http://jqueryui.com/autocomplete/#custom-data
如果没有,则将ui-menu-item-wrapper 类分配给每个元素,从而造成严重破坏。