【问题标题】:Strict mode error only in IE11. Can I take off strict mode just for this browser?仅在 IE11 中出现严格模式错误。我可以只为这个浏览器取消严格模式吗?
【发布时间】:2017-08-20 15:29:30
【问题描述】:

我收到错误`Assignment to read-only properties is not allowed in strict mode' 在我的代码的这一行:

wrapper.style = 'transition: transform 3s;';

这是它所在函数的一部分:

'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' );
  }
}

这段代码在我测试过的所有现代浏览器中都能完美运行。只有在 IE11 中,这会破坏整个站点,停止所有后续行的运行。

如果我取出第一行:'use strict'; 一切正常。 在保持严格模式的同时有一个简单的修复吗?或者只是针对 IE11 并以某种方式删除该浏览器的严格模式?

可能有更好的方法。

【问题讨论】:

  • 你可以试试这个包装器['style'] = 'transition: transform 3s;';

标签: javascript css internet-explorer-11


【解决方案1】:

结帐:

https://devtidbits.com/2016/06/12/assignment-to-read-only-properties-is-not-allowed-in-strict-mode/#comment-1611

显然你应该使用类似的东西

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.

但是,即使应用了该修复程序,我仍然会遇到相同的错误...

【讨论】:

    【解决方案2】:

    我认为您可能会遇到错误,因为您尝试将 transform 3s 分配给样式属性,而实际上您应该将其分配给过渡属性。 像这样: wrapper.style.transition = 'transform 3s';

    【讨论】:

      【解决方案3】:

      为什么不修复它,而不是忽略错误?

      wrapper.style.transition = 'transform 3s;';
      

      【讨论】:

      • 我原始代码中的方式适用于所有现代浏览器。但是,我的代码在这里或 IE11 有问题吗?
      • 那是因为大多数现代浏览器have a workaround built in。从技术上讲,style 属性返回 CSSStyleDeclaration 并且是只读的。
      猜你喜欢
      • 1970-01-01
      • 2011-08-26
      • 2014-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-19
      相关资源
      最近更新 更多