html下拉框的宽度怎么设置,在HTML选择下拉选项中设置下拉元素的宽度

HTML:

Please select an item

Item text goes in here but it is way too long to fit inside a select option that has a fixed width adding more

CSS:

.shortenedSelect {

max-width: 350px;

}使用Javascript:

// Shorten select option text if it stretches beyond max-width of select element

$.each($('.shortenedSelect option'), function(key, optionElement) {

var curText = $(optionElement).text();

$(this).attr('title', curText);

// Tip: parseInt('350px', 10) removes the 'px' by forcing parseInt to use a base ten numbering system.

var lengthToShortenTo = Math.round(parseInt($(this).parent('select').css('max-width'), 10)/7.3);

if (curText.length > lengthToShortenTo) {

$(this).text('... ' + curText.substring((curText.length - lengthToShortenTo), curText.length));

}

});

// Show full name in tooltip after choosing an option

$('.shortenedSelect').change(function() {

$(this).attr('title', ($(this).find('option:eq('+$(this).get(0).selectedIndex +')').attr('title')));

});完美的工作。我自己也有同样的问题。看看这个JSFiddle http://jsfiddle.net/jNWS6/426/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值