jquery - 修复IE中不写 'px or %'的行内样式的宽度

标签 jquery css internet-explorer

我必须修复没有“px 或 %”的行内样式的宽度。 无法修改页面中的html和css,只能修改脚本 我想读取宽度(仅使用 javascript)来修复缺少的单位测量值。

问题仅出现在 IE 9 上。在 chrome、FF、safari 中,我可以使用下面的代码调整宽度。

使用 JQuery 1.8.2

/*script in head */
$(function() {
       var style = $("#test").attr("style"); 
       alert(style);
       //in IE it prints only color:red;border:1px solid red;
});

/* html in body */
<div id="test" style="width:20;color:red;border:1px solid red;">
    Content ...
</div>

tis 问题(IE 问题)的一个例子-> http://jsfiddle.net/2QJ96/1/

最佳答案

你可以通过读取宽度

$('#test').css('width');

此外,您的 HTML 需要修改:

/* html in body */
<div id="test" style="width:20px; color:red; border:1px solid red;">
    Content ...
</div>

关于jquery - 修复IE中不写 'px or %'的行内样式的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12747148/

相关文章:

javascript - jquery 在动态创建的 div 中选择动态类 ID

jQuery 常量作为调用函数的变量

html - Bootstrap Nav + Carousel + Footer 保持相对高度

html - 带有背景的 CSS 旋转文本显示了一些背景问题

html - IE 9 中的兼容模式

css - 过渡在 IE 中不起作用

jQuery UI slider : how to inline it?

javascript - 从 javascript 设置隐藏字段值,但没有在后面的代码中获取它

CSS 父/祖先选择器

internet-explorer - Google Analytics(分析)在IE中无法运作