简体   繁体   English

如何使用jQuery更改选项框的宽度(独立于选择宽度)?

[英]How to change the width of option box (independet of select width) with jquery?

How could I set the width of option box different than select width? 如何将选项框的宽度设置为与选择宽度不同?

what I tried: 我试过的

$('option').css({'width': 100});

and the select hast to be: 而选择必须是:

select{
width:400px;
}

Fiddle: http://jsfiddle.net/7k8He/1/ 小提琴: http : //jsfiddle.net/7k8He/1/

Option text wants to be shorter but option box not..Is it possible to change that dynamically 选项文字要短些,但选项框不要短..是否可以动态更改

Thanx in advance! 提前感谢!

You can't set the width for the option element. 您不能设置option元素的宽度。 The browser will automatically make all the options the width of the longest option. 浏览器将自动使所有选项成为最长选项的宽度。

You can use the 'css' selector like this: 您可以像这样使用“ css”选择器:

$('option').css({'width','100px'});

or set it with the width selector like this: 或使用宽度选择器设置它,如下所示:

$('option').width(100);

https://api.jquery.com/width/ https://api.jquery.com/width/

You should wright $('select option').css('width','250'); 您应该使用$('select option').css('width','250'); like this syntax is bit wrong 这样的语法有点错误

Demo 演示版

if you want to set multiple properties you can use something like $('select option').css({"border":"1px solid red", "height":35}); 如果要设置多个属性,则可以使用$('select option').css({"border":"1px solid red", "height":35});

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM