[英]Strict mode error only in IE11. Can I take off strict mode just for this browser?
I get the error `Assignment to read-only properties is not allowed in strict mode' on this line of my code: 我在代码的这一行收到错误消息“在严格模式下不允许向只读属性分配” :
wrapper.style = 'transition: transform 3s;';
This is a chunk of the function it's in: 这是它所在功能的一部分:
'use strict';
function work() {
var wrapper = document.getElementById( 'wrp' ),
infoPage = document.getElementById( 'i-pg' ),
body = document.getElementById( 'body' ),
carA = document.getElementById( 'car-a' ),
keyA = document.getElementById( 'key-a' ),
manualA = document.getElementById( 'manual-a' ),
wheelA = document.getElementById( 'wheel-a' );
if( this.id === 'info' ) {
wrapper.style = 'transition: transform 3s;'; //PROBLEM LINE
wrapper.classList.add( 'up' );
body.classList.add( 'dark' );
infoPage.classList.remove( 'down' );
}
}
This code works perfectly in all modern browsers i've tested. 该代码在我测试过的所有现代浏览器中均能完美运行。 Only in IE11 is this breaking the entire site, stopping all subsequent lines from running. 仅在IE11中,这会破坏整个站点,并停止所有后续行的运行。
If I take out the first line: 'use strict';
如果我删除第一行: 'use strict';
everything works fine. 一切正常。 Is there a simple fix while keeping strict mode on? 在保持严格模式的同时,有没有简单的解决方法? Or just target IE11 and somehow remove strict mode for that browser? 还是只针对IE11并以某种方式删除该浏览器的严格模式?
There might be a better approach. 可能有更好的方法。
为什么不修复它,而不是忽略错误?
wrapper.style.transition = 'transform 3s;';
I think you might get an errror cause you're trying to assign a transform 3s
to style property, while actually you should assign it to a transition property. 我认为您可能会犯错,因为您试图将transform 3s
分配给样式属性,而实际上您应该将其分配给转换属性。 Like this: wrapper.style.transition = 'transform 3s'; 像这样:wrapper.style.transition ='transform 3s';
Checkout: 查看:
https://devtidbits.com/2016/06/12/assignment-to-read-only-properties-is-not-allowed-in-strict-mode/#comment-1611 https://devtidbits.com/2016/06/12/assignment-to-read-only-properties-is-not-allowed-in-strict-mode/#comment-1611
Apparently you're supposed to use this something like 显然,您应该使用类似
myEle.style.cssText = "color: red;" // or
myEle.setAttribute("class", "myClass"); // Multiple style properties
//For newly created style sheet objects, you must first append the element to the document before you can set cssText.
However I still get the same error even after applying that fix... 但是即使应用该修复程序,我仍然会收到相同的错误...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.