我正在使用 jQuery UI自动完成插件 (版本1.8),我想自定义建议的方式。具体来说,我不仅要显示一些文本,还要显示一个图标。但是,当我发送u003Cimg>标签,它只是在结果列表中被渲染为纯文本。

有其他方法可以改变这种行为吗?另外,您还能提出一种不同的方法来在返回的结果中包含图像并在建议中显示它们吗?

有帮助吗?

解决方案

???

??
$("#search_input").autocomplete({source: "/search",
                                 minLength: 3,
                                 select: function (event, ui) {
                                     document.location = ui.item.url;
                                 }
})
.data("autocomplete")._renderItem = function (ul, item) {
    return $('<li class="ui-menu-item-with-icon"></li>')
        .data("item.autocomplete", item)
        .append('<a><span class="' + item.type + '-item-icon"></span>' + item.label + '</a>')
        .appendTo(ul);
};

??CSS:

.ui-menu .ui-menu-item-with-icon a {
  padding-left: 20px;

}
span.group-item-icon,
span.file-item-icon {
  display: inline-block;
  height: 16px;
  width: 16px;
  margin-left: -16px;
}
span.group-item-icon {
  background: url("/image/icons/group.png") no-repeat left 4px;
}
span.product-item-icon {
  background: url("/image/icons/product.png") no-repeat left 7px;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top