简体   繁体   English

jQuery通过更改属性更改元素的预定义样式在IE中不起作用

[英]JQuery changing element's predefined style by changing attribute doesn't work in ie

I have a few predefined styles in my css based on an elements attribute. 我的CSS中有一些基于elements属性的预定义样式。 If i update the attribute's value to something new that also has a predefined style the style is applied in FF and Chrome but not IE. 如果我将属性的值更新为也具有预定义样式的新内容,则该样式将应用于FF和Chrome,但不会应用于IE。 See example and type in input field please. 请参阅示例,然后在输入字段中键入。

JS: JS:

$('input').on('keypress', function(){
    var l = parseInt($(this).attr('level'), 10) + 1;
    $(this).attr('level', l);
});

CSS: CSS:

input[level="0"]{background:red;}
input[level="1"]{background:blue;}
input[level="2"]{background:green;}
input[level="3"]{background:black;}

http://jsfiddle.net/ce6S4/ http://jsfiddle.net/ce6S4/

Any help is appreciated, thank you. 任何帮助表示赞赏,谢谢。

EDIT: works with IE 9 but not <= 8 编辑:与IE 9一起使用,但不<= 8

It's an IE-bug. 这是一个IE错误。 Notice that the element is rendered correctly when you resize the window. 请注意,调整窗口大小时,元素将正确呈现。

Trigger the reflow by setting the class name: 通过设置类名称来触发重排:

this.className = this.className; // Or .addClass(' ');

Demo: http://jsfiddle.net/ce6S4/6/ 演示: http//jsfiddle.net/ce6S4/6/

$('input').on('keypress', function(){
    var l = parseInt($(this).attr('level'), 10) + 1;
    $(this).attr('level', l).addClass(' ');
});

http://jsfiddle.net/ce6S4/3/ http://jsfiddle.net/ce6S4/3/

try this 尝试这个

   $('input').on('keydown', function(){
         var l = parseInt($(this).attr('level'), 10) + 1;
    alert(l); //can be removed.
         $(this).attr('level', l.toString()).hide().show().focus();

});​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM