jQuery选择样式自动宽度

jQuery选择样式自动宽度,jquery,select,width,option,Jquery,Select,Width,Option,我正在使用此选择样式脚本: 有没有办法使新的选择器div width适应文本长度?目前,它必须是css中定义的宽度 滴水器也是一样吗 我尝试过调整css,但我认为这可能只适用于jquery,但我不知道如何编辑该文件以使其正常工作 有什么想法吗 下面是一个应该适合您的实现 $("#country_id").selectbox({ onOpen: function (inst) { //there is no onCreated handler, so I have to u

我正在使用此选择样式脚本:

有没有办法使新的选择器div width适应文本长度?目前,它必须是css中定义的宽度

滴水器也是一样吗

我尝试过调整css,但我认为这可能只适用于jquery,但我不知道如何编辑该文件以使其正常工作


有什么想法吗

下面是一个应该适合您的实现

$("#country_id").selectbox({
    onOpen: function (inst) {
        //there is no onCreated handler, so I have to use this.
        //but if you want you can just fire this somewhere else
        go();
        //console.log("open", inst);
    },
    onClose: function (inst) {
        //console.log("close", inst);
    },
    onChange: function (val, inst) {
        console.log(val);
    },
    effect: "slide"
});

function go(){
    var widestLI = 0;
    //iterate through each LI and get the widest one
    $(".sbOptions > li").each(function(){
        var tmpWidth = $(this).textWidth();
        if (tmpWidth > widestLI) widestLI = tmpWidth;
        console.log(widestLI);
    });
    //now set both these elements to the widest width, plus 20px because of the padding
    $(".sbOptions, .sbHolder").width(widestLI+20);
}

//from http://stackoverflow.com/questions/1582534/calculating-text-width-with-jquery    
//this here gets the width of the html element.                                      
$.fn.textWidth = function(){
    var html_calc = $('<span>' + $(this).html() + '</span>');
    html_calc.css('font-size',$(this).css('font-size')).hide();
    html_calc.prependTo('body');
    var width = html_calc.width();
    html_calc.remove();
    return width;
}​