IE
我编写了一个函数,它接受
有没有办法强制
我使用的是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为务实的解决方案是替换整个
如果替换整个选择,需要记住的是,在将被替换元素从 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.
可以通过css设置其宽度。
在你的情况下,我会使用这种方法来处理它:
如果你使用 em 或 ex 作为字体大小单位,那么由于它是基于高度的值,你可以假设使用一定的比率,它几乎接近于字符的等值宽度。基于此,您实际上可以通过以下方式计算选择元素的宽度:
1st:从选择选项中获取最小字符串的字符数。
第二:将其乘以 em 值。
第三:将此值设置为选择的宽度。
使用 jquery 可以通过以下方式实现:
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: