【问题标题】:Why does Jasmine complain about 'Attempted to assign to readonly property' when I set selectedIndex?当我设置 selectedIndex 时,为什么 Jasmine 会抱怨“尝试分配给只读属性”?
【发布时间】:2016-08-20 08:10:15
【问题描述】:

当我通过 grunt-contrib-jasmine 运行这个 Jasmine 规范时,我收到错误 TypeError: Attempted to assign to readonly property. 指向我设置 selectedIndex 属性的行:

it('does something', function () {
    setFixtures('<select id="selectId"><option>Text</option></select>');
    var selectDOM = document.getElementById('selectId');
    selectDOM.selectedIndex = 0;
    // stripped some code here...
});

有人知道我在这里缺少什么吗? 我使用 phantomjs v1.9.8grunt-cli v0.1.13grunt-contrib-jasmine v0.9.2

【问题讨论】:

  • 在这个例子中我没有打错字,所以这个例子可以正常运行。如果您似乎有同样的问题,请阅读下面的答案。

标签: javascript jasmine grunt-contrib-jasmine


【解决方案1】:

知道了,我犯了一个错误,但由于这很难找到,所以我现在将我的问题和答案留在这里。不过,请随意将其完全删除。

我在传递给 getElementById() 函数的值中打错了,因此变量 selectDOM 为空。由于某种原因,这导致了错误“TypeError: Attempted to assign to readonly property.”。

我很难看到错字,并且在这种情况下错误消息具有误导性,因此其他人可能会遇到同样的问题。

【讨论】:

    猜你喜欢
    • 2020-01-14
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 2013-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多