【问题标题】:"Use Strict" breaking Javascript function - Why?“使用严格”破坏 Javascript 功能 - 为什么?
【发布时间】:2013-04-30 00:04:43
【问题描述】:

向这个函数添加“use strict”会破坏它。具体来说,我试图取消禁用的输入在严格模式下仍然被禁用。 csc_popup_in() 和 csc_popup_out() 函数在此之前定义。提前感谢您的任何意见。

function show_cc(){
    document.getElementById('cc_fade').className='none';

    var cc_inps = document.getElementById('cc_fade').getElementsByTagName('input');
    for(i=0 ; i<cc_inps.length ; i++){
        cc_inps[i].disabled=false;
    }

    document.getElementById('csclnk').onmouseover = function(){csc_popup_in();};
    document.getElementById('csclnk').onmouseout = function(){csc_popup_out();};

    if(document.getElementById('amex').checked){
        document.getElementById('cc_num').maxLength = 15;
        document.getElementById('cc_csc').maxLength = 4;
    } else {
        document.getElementById('cc_num').maxLength = 16;
        document.getElementById('cc_csc').maxLength = 3;
    }
}

【问题讨论】:

  • 你的 JavaScript 控制台肯定报错了...
  • 正如 Pointy 建议的那样,您应该寻找错误。严格模式的部分好处是您可能会收到 more 错误,告诉您代码中有问题。这是一件好事。
  • 也可以,但是你有不必要的匿名函数。您可以直接将函数分配为处理程序。例如:document.getElementById('csclnk').onmouseover = csc_popup_in;
  • +1 对处理程序的建议,谢谢!

标签: javascript strict


【解决方案1】:

你忘了用var声明“i”!!!

【讨论】:

  • 知道这很愚蠢。我是自学成才的,所以我的调试方法不是最好的。我将按照建议努力改进 JS 控制台。谢谢!
猜你喜欢
  • 1970-01-01
  • 2016-08-16
  • 1970-01-01
  • 2011-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-16
相关资源
最近更新 更多