CSS1Compat模式下IE8+ onpropertychange bug
CSS1Compat模式下IE,给输入控件注册了onpropertychange事件后,如果在事件中修改了控件的值,IE下会再次触发onpropertychange事件,但是此时输入内容,不会响应onpropertychange事件了,再输入一个字符才会响应,如此重复。firefox下对应的oninput事件,对javascript动态设置控件内容不会响应oninput事件,IE则会响应onpropertychange事件。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml-transitional.dtd"> <html> <head> <script type="text/javascript"> var oldval; function mytextest(V) { alert(V.value); if (V.value != oldval) { oldval = V.value; V.value = V.value;//firefox下动态设置控件内容不会响应oninput事件,IE会响应 } } </script> </head> <body> <input type="text" onpropertychange="mytextest(this)" oninput="mytextest(this)" /> </body> </html>
如上面的示例,输入a,会alert 2次a,再输入b没有响应,再输入c,输出2此abc,再输入d没响应。。
google了下http://stackoverflow.com/questions/12557511/is-this-a-bug-with-internet-explorer-8s-onpropertychange-event也有问过此问题,但是没有提到解决办法,后面自己鼓捣了下,找到了2种解决办法。
解决办法有2个
1)去掉xhtml申明则没有问题(不推荐,会导致原来的页面布局错乱)
2)通过X-UA-Compatible将兼容模式修改为IE7,如下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml-transitional.dtd"> <html> <head> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> <script type="text/javascript"> var oldval; function mytextest(V) { alert(V.value); if (V.value != oldval) { oldval = V.value; V.value = V.value; } } </script> </head> <body> <input type="text" onpropertychange="mytextest(this)" oninput="mytextest(this)" /> </body> </html>
加支付宝好友偷能量挖...
原创文章,转载请注明出处:CSS1Compat模式下IE8+ onpropertychange bug