使用jquery/js查找元素的自动宽度
在 IE 中,在 Focus 上,仅当自动宽度比我设置的静态宽度宽时,我才可能将元素的宽度扩展为 width: auto
。我试图避免的是总是将元素设置为 width: auto
并让元素收缩。我只希望它在必要时扩展。
任何人都有一种干净的方法来检查元素宽度设置为 auto 时的宽度,而无需先设置属性?现在,我设置它,然后检查宽度并可能将其设置回静态,似乎应该有一个更干净的解决方案。
当前使用 jquery 的代码示例:
$('mySelector').live('focus', function() {
//store the original width, then set the element to width: auto
$(this).data('w', $(this).width()).css('width', 'auto'));
//is the element now shorter? if so, set it back to the original width
if ($(this).width() < $(this).data('w')) {
$(this).width($(this).data('w'));
}
}
In IE, on Focus I want to potentially expand the width of an element to width: auto
only where the automatic width is wider then the static width I have set. What Im trying to avoid is just always setting the element to width: auto
and having the element shrink. I only want it to expand where necessary.
Anyone have a clean way to check what the elements width would be when set to auto with out having to first set the attribute? Right now, I set it, then check the width and potentially set it back to static, seems like there should be a cleaner solution.
Example of current code with jquery:
$('mySelector').live('focus', function() {
//store the original width, then set the element to width: auto
$(this).data('w', $(this).width()).css('width', 'auto'));
//is the element now shorter? if so, set it back to the original width
if ($(this).width() < $(this).data('w')) {
$(this).width($(this).data('w'));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定您是否会找到更干净的解决方案,但这是一个不涉及操作元素本身的解决方案:
Not sure if you will find a cleaner solution, but here's one that doesn't involve manipulating the element itself: