开发者

JQuery AutoComplete - Styling the extra data (i.e. Description)

开发者 https://www.devze.com 2023-03-18 08:21 出处:网络
I am using the JQuery library\'s autocomplete function and wanted to know how I could CSS style the extra information (description).

I am using the JQuery library's autocomplete function and wanted to know how I could CSS style the extra information (description).

i.e. My current results when typing into the input is: 开发者_StackOverflow中文版John Smith www.johnsmith.com

I want to make the domain names be styled in a smaller font, italics, and color. I tried adding this CSS as shown in the example on jquery.com:

#result-description {
    margin: 0;
    padding: 0;
    font-style:italic;
    font-size:3px !important;
}

But no luck!

Any suggestions?


Have you seen this example: http://jqueryui.com/demos/autocomplete/#custom-data?

It shows perfectly how to do it:

<style>
    #project-label {
        display: block;
        font-weight: bold;
        margin-bottom: 1em;
    }
    #project-icon {
        float: left;
        height: 32px;
        width: 32px;
    }
    #project-description {
        margin: 0;
        padding: 0;
    }
    </style>
    <script>
    $(function() {
        var projects = [
            {
                value: "jquery",
                label: "jQuery",
                desc: "the write less, do more, JavaScript library",
                icon: "jquery_32x32.png"
            },
            {
                value: "jquery-ui",
                label: "jQuery UI",
                desc: "the official user interface library for jQuery",
                icon: "jqueryui_32x32.png"
            },
            {
                value: "sizzlejs",
                label: "Sizzle JS",
                desc: "a pure-JavaScript CSS selector engine",
                icon: "sizzlejs_32x32.png"
            }
        ];

        $( "#project" ).autocomplete({
            minLength: 0,
            source: projects,
            focus: function( event, ui ) {
                $( "#project" ).val( ui.item.label );
                return false;
            },
            select: function( event, ui ) {
                $( "#project" ).val( ui.item.label );
                $( "#project-id" ).val( ui.item.value );
                $( "#project-description" ).html( ui.item.desc );
                $( "#project-icon" ).attr( "src", "images/" + ui.item.icon );

                return false;
            }
        })
        .data( "autocomplete" )._renderItem = function( ul, item ) {
            return $( "<li></li>" )
                .data( "item.autocomplete", item )
                .append( "<a>" + item.label + "<br>" + item.desc + "</a>" )
                .appendTo( ul );
        };
    });
    </script>



<div class="demo">
    <div id="project-label">Select a project (type "j" for a start):</div>
    <img id="project-icon" src="/demos/autocomplete/images/transparent_1x1.png"  class="ui-state-default"//>
    <input id="project"/>
    <input type="hidden" id="project-id"/>
    <p id="project-description"></p>
</div><!-- End demo -->


Finally solved it! Have to add a font tag (with class of your styling) to the the .append portion of the autocomplete:

.data( "autocomplete" )._renderItem = function( ul, item ) {
        return $( "<li></li>" )
            .data( "item.autocomplete", item )
            .append( "<a>" + item.label + "<br><font class=\"description\">" + item.desc + "</font></a>" )
            .appendTo( ul );
    };
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号