IE中的jQuery .width(val)错误 - 参数无效 [英] jQuery .width(val) error in IE - invalid argument

查看:102
本文介绍了IE中的jQuery .width(val)错误 - 参数无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过ajax加载内部div(#book_table)后,我想调整主体的宽度以适应更大的内容。

After loading an internal div (#book_table) via ajax, I want to resize the width of the body to accommodate the larger content.

var new_width = parseInt($('#book_table').css('width'))+407;
$('body').width(new_width);

适用于FF和Safari,但在IE中使用无效参数失败。在解压缩的jQuery 1.3.1中,它的第1049行:

Works in FF and Safari, but fails in IE with "Invalid argument". In the unpacked jQuery 1.3.1, it's line 1049:

elem[ name ] = value;

但是,传递文字值可以在IE中使用:

Passing a literal value, however, works in IE:

$('body').width(1200);


推荐答案

你应该试试:

var new_width = $('#book_table').width() + 407;
$('body').width(new_width);

使用 css('width')是返回带有'px'扩展名的字符串,而不仅仅是一个数字。 Firefox可以使用 parseInt 100px 100 >,但看起来IE没有。

Using css('width') is returning a string with the 'px' extension rather than simply a number. Firefox can properly parse "100px" to be 100 with parseInt, but it looks like IE does not.

使用 width()只返回一个整数,无需调用 parseInt

Using width() will just return an integer, which also removes the need to call parseInt.

这篇关于IE中的jQuery .width(val)错误 - 参数无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆