IE

发布于 2024-10-04 07:06:48 字数 240 浏览 3 评论 0原文

我编写了一个函数,它接受

有没有办法强制

我使用的是 IE 8。

I've written a function which takes a <select> ID, and replaces all the options with new ones (using jQuery's html()). However, on IE the width of the dropdown remains the original one which has a size way bigger than what I need to display, because is based on the original <option>'s that had long strings captions.

Is there a way to force the <select> to recalculate it's width?

I am using IE 8.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心房的律动 2024-10-11 07:06:48

我认为务实的解决方案是替换整个

如果替换整个选择,需要记住的是,在将被替换元素从 DOM 中删除(以避免任何潜在的内存泄漏)和附加到新插入元素的事件处理程序之前,需要清除对被替换元素的任何引用。

I think the pragmatic solution would be to replace the whole <select> element. This way, the newly inserted one will have the correct width for its <option> elements (disregarding any other styling that may be applied).

The thing to bear in mind if replacing the whole select is that any references to the replaced element will need to cleaned up prior to removing it from the DOM (to avoid any potential memory leaks) and event handlers attached to the newly inserted element.

最近可好 2024-10-11 07:06:48

可以通过css设置其宽度。
在你的情况下,我会使用这种方法来处理它:

如果你使用 em 或 ex 作为字体大小单位,那么由于它是基于高度的值,你可以假设使用一定的比率,它几乎接近于字符的等值宽度。基于此,您实际上可以通过以下方式计算选择元素的宽度:

1st:从选择选项中获取最小字符串的字符数。
第二:将其乘以 em 值。
第三:将此值设置为选择的宽度。

使用 jquery 可以通过以下方式实现:

var em = 1em; //assign here a value, that corresponds to your layout's font size
var shortest = 6500;
$('mySelect').each(function(i, selected){
   temp = $(selected).text().length();
   shortest = (temp < shortest) ? temp : shortest;
});
$('mySelect').css('width',(shortest*em + 1) + 'em'); //add 1em for the scrollbar

It's possible to set its width through css.
In your case I would approach it using this method:

If you are using em's or ex's as font size units, then since it is a value based on height, you can assume using a certain ratio that it is almost near the equivalent of a character's width. Based on that, you can actually calculate how wide your select element would be by:

1st: get the number of characters of the smallest string from the select options.
2nd: multiply that by the em value.
3rd: set this value as the select's width.

using jquery that would be achieved through:

var em = 1em; //assign here a value, that corresponds to your layout's font size
var shortest = 6500;
$('mySelect').each(function(i, selected){
   temp = $(selected).text().length();
   shortest = (temp < shortest) ? temp : shortest;
});
$('mySelect').css('width',(shortest*em + 1) + 'em'); //add 1em for the scrollbar
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文