简体   繁体   English

严格模式错误仅在IE11中。 我可以仅为此浏览器取消严格模式吗?

[英]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.

相关问题 在 Angular 中关闭严格模式? - Turning off Strict Mode in Angular? 严格模式IE11中不允许分配只读属性 - Assignment to read-only properties is not allowed in strict mode IE11 在元素上的ie11 / Edge中设置宽度-在严格模式下不允许分配只读属性 - Setting width in ie11/Edge on element - assignment to read-only properties is not allowed in strict mode 在 ie 11 中,严格模式下不允许 AngularJS 分配给只读属性 - AngularJS Assignment to read-only properties is not allowed in strict mode in ie 11 “函数声明不能​​嵌套在语句中” - IE 在严格模式下的错误 - “Function declarations cannot be nested inside a statement” - IE in strict mode error 如何在严格模式下定义[this]? - How can [this] be undefined in strict mode? 如何在不删除 react index.js 文件中的严格模式的情况下解决严格模式警告 - How can I solve strict mode warning without removing strict mode in react index.js file IE6严格模式和顶部:0,底部:0 - IE6 Strict Mode & Top:0, Bottom:0 Vue 错误:在严格模式代码中,函数只能在顶层或块内声明 - Vue error: In strict mode code, functions can only be declared at top level or inside a block 我应该担心“窗口没有定义”JSLint严格模式错误? - Should I worry about “window is not defined” JSLint strict mode error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM