Вопрос по jquery – jQuery - получить ширину опции в списке выбора?

1

Error: User Rate Limit Exceeded

$('#mySelect').width();

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded Evanss
Error: User Rate Limit ExceedediiiiiiiiiiError: User Rate Limit ExceededMMMMMMMMMMError: User Rate Limit Exceeded<option>Error: User Rate Limit Exceeded Wesley Murch
Error: User Rate Limit Exceeded gdoron
Error: User Rate Limit Exceeded Wesley Murch
Error: User Rate Limit Exceeded NimChimpsky

Ваш Ответ

4   ответа
0

$('#mySelect > option').width();

Error: User Rate Limit Exceeded<option>Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded<select>Error: User Rate Limit Exceededid="mySelect"Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededwidth:auto;Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded Evanss
Error: User Rate Limit Exceeded<option>Error: User Rate Limit ExceededvalueError: User Rate Limit Exceeded$('#mySelect').val()
1

make a new <span> object set text to the one of the option in question get computed style values relevant for text width from the selected option set those measured values to the span append the span to the body measure the width of that span remove the span


var getOptionWidth = function(opt) {
    var styleProps = [
        'font-family',
        'font-kerning',
        'font-size',
        'font-stretch',
        'font-style',
        'font-variant',
        'font-variant-ligatures',
        'font-weight',
        'text-transform',
        'letter-spacing',
        'word-spacing',
        'padding-left',
        'padding-right'
    ];
    var o = $('<span>')
        .text($(opt).text());
    for (var i=0; i < styleProps.length; i++) {
        o.css(styleProps[i], $(opt).css(styleProps[i]));
    }
    o.appendTo('body');
    var w = o.width();
    o.remove();
    return w;
}
0

 ("#mySelect option:selected").text().length

$("#mySelect option[value='2']").text().length

Error: User Rate Limit Exceeded Evanss
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
0

<select id="hack1">
    <option>short option</option>
    <option>loooong looking loooong option</option>
    <option>Longer option but more like the longest</option>
    <option>Medium length selector</option>
</select>

<select id="hackselect">
    <option></option>
</select>

#hackselect {
    visibility: hidden;
}

$('#hack1').change(function(event) {
    $("#hackselect option:first").text($("#hack1 option:selected").text());
    $('#hack1').css('width', $('#hackselect').width() + 6);
});

Похожие вопросы