【问题标题】:Switch case in Javascript is not working in IE8Javascript 中的切换大小写在 IE8 中不起作用
【发布时间】:2012-07-21 14:38:37
【问题描述】:

代码sn-p:

var role=s[0].Role;// role contains string value 
dijit.byId("editRole").attr("Value",getRoleByName(role));

function getRoleByName(role)
{
    var roleVal;
    alert(role);
    switch(role)
    {
        case 'Basic User' :roleVal='1';break;
        case 'Network Operator' :roleVal='3';break;
        case 'System Administrator' :roleVal='5';break;
        case 'Custom Level 1' :roleVal='11';break;
        case 'Custom Level 2' : roleVal='12';break;
        default: roleVal='1';break;
    }
    return roleVal;
}

当我尝试调用其中包含 switch 语句的 Javascript 方法时,我在 IE8 中遇到错误,但在 Firefox 中工作正常..

开发者工具中的错误:

method Error executing: function(/*Event*/ e){
    // summary:
    // Handler when the user activates the button portion.
    if(this._onClick(e) === false){ // returning nothing is same as true
        e.preventDefault(); // needed for checkbox
    } else if (this.type == "submit" && !this.focusNode.form){ // see if a nonform widget needs to be signalled
        for(var node=this.domNode; node.parentNode/*#5935*/; node=node.parentNode){
            var widget=dijit.byNode(node);
            if(widget && typeof widget._onSubmit == "function"){
                widget._onSubmit(e);
                break;
            }
        }
    }
}TypeError: Object doesn't support this property or method

谁能帮我解决这个问题? ...如何克服这个问题?...

【问题讨论】:

    标签: javascript dojo internet-explorer-8 dom-events dojox.grid.datagrid


    【解决方案1】:

    您显示的错误与以下代码无关。正如 dojotoolkit-src 的评论亲切地向您展示的那样,它与函数 '当用户激活按钮部分时的处理程序有关。'

    应该解决这个问题

     // << Value is all lower case
     dijit.byId("editRole").attr("Value",getRoleByName(role)); 
    

    还有一个带有“WTF”的更简洁的编码开关——我认为这可能是你在这里试验的语法突出显示——或者这些波浪线真的在你的代码中吗?

     // What are these back-tilds doing here?
     switch(role)``  
     {
          case 'Basic User'           : return 1
          case 'Network Operator'     : return 3
          case 'System Administrator' : return 5
          case 'Custom Level 1'       : return 11
          case 'Custom Level 2'       : return 12
          default:
               return 1
         // return works as your break does, it stops the switch
    }
    

    在修剪过程中说出您的击球问题?试试这个,不是所有的javascript引擎都实现了trim

    if(typeof String.prototype.trim !== 'function') {
            String.prototype.trim = function() {
                    return this.replace(/^\s+|\s+$/g, ''); 
            }
    }
    

    【讨论】:

    • 感谢 mschr 的及时回复......你是对的......实际上问题是我正在使用 role.toString().trim()。这导致了问题......但如果我删除 toString 转换,它总是返回默认值。
    • trim 不是一个完全本机实现的功能,那么请在我的答案中添加一个字符串原型扩展
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    相关资源
    最近更新 更多