将自定义类添加到jQuery UI的ui-autocomplete Combobox Div

时间:2022-11-13 17:45:01

How do I add a custom class to the ui-autocomplete div? I have multiple autocomplete widgets loading on my page and some of their drop downs need to be styled differently so I can't just edit the ui-autocomplete class. I am working with the jQuery UI combobox code (http://jqueryui.com/autocomplete/#combobox) and, by altering that code, I would like to add a class to the created ui-autocomplete div.

如何向ui-autocomplete div添加自定义类?我在我的页面上加载了多个自动完成小部件,并且他们的一些下拉菜单需要设置不同的样式,所以我不能只编辑ui-autocomplete类。我正在使用jQuery UI组合框代码(http://jqueryui.com/autocomplete/#combobox),并且通过更改该代码,我想在创建的ui-autocomplete div中添加一个类。

4 个解决方案

#1


20  

$("#auto").autocomplete({
    source: ...
}).autocomplete( "widget" ).addClass( "your_custom_class" );

#2


13  

Sorry for delay). Have a look at code below.

不好意思推迟了)。看看下面的代码。

$(function () {
    $("#auto").autocomplete({
        source: ['aa', 'bb', 'cc', 'dd']
    }).data("ui-autocomplete")._renderItem = function (ul, item) {

        ul.addClass('customClass'); //Ul custom class here

        return $("<li></li>")
        .addClass(item.customClass) //item based custom class to li here
        .append("<a href='#'>" + item.label + "</a>")
        .data("ui-autocomplete-item", item)
        .appendTo(ul);
    };
});

#3


5  

Simply use the classes argument:

只需使用classes参数:

$("#auto").autocomplete({
    classes: {
        "ui-autocomplete": "your-custom-class",
    },
    ...
});

This means that wherever jQuery UI applies the ui-autocomplete class it should also apply your-custom-class.

这意味着无论jQuery UI应用ui-autocomplete类,它都应该应用你的自定义类。

This is relevant to any jQuery UI widget, not just Autocomplete. See the documentation.

这与任何jQuery UI小部件相关,而不仅仅是自动完成。请参阅文档。

#4


0  

Use this method to add custom classes to drop down box

使用此方法将自定义类添加到下拉框

_renderMenu: function( ul, items ) {
    var that = this;
    $.each( items, function( index, item ) {
        that._renderItemData( ul, item );
    });
    $( ul ).find( "li:odd" ).addClass( "odd" );
}

#1


20  

$("#auto").autocomplete({
    source: ...
}).autocomplete( "widget" ).addClass( "your_custom_class" );

#2


13  

Sorry for delay). Have a look at code below.

不好意思推迟了)。看看下面的代码。

$(function () {
    $("#auto").autocomplete({
        source: ['aa', 'bb', 'cc', 'dd']
    }).data("ui-autocomplete")._renderItem = function (ul, item) {

        ul.addClass('customClass'); //Ul custom class here

        return $("<li></li>")
        .addClass(item.customClass) //item based custom class to li here
        .append("<a href='#'>" + item.label + "</a>")
        .data("ui-autocomplete-item", item)
        .appendTo(ul);
    };
});

#3


5  

Simply use the classes argument:

只需使用classes参数:

$("#auto").autocomplete({
    classes: {
        "ui-autocomplete": "your-custom-class",
    },
    ...
});

This means that wherever jQuery UI applies the ui-autocomplete class it should also apply your-custom-class.

这意味着无论jQuery UI应用ui-autocomplete类,它都应该应用你的自定义类。

This is relevant to any jQuery UI widget, not just Autocomplete. See the documentation.

这与任何jQuery UI小部件相关,而不仅仅是自动完成。请参阅文档。

#4


0  

Use this method to add custom classes to drop down box

使用此方法将自定义类添加到下拉框

_renderMenu: function( ul, items ) {
    var that = this;
    $.each( items, function( index, item ) {
        that._renderItemData( ul, item );
    });
    $( ul ).find( "li:odd" ).addClass( "odd" );
}