var name = "全局变量"; $(function () { function test(){ var name = "局部变量"; alert(name); // 局部变量 eval( "alert(name);" ); // 局部变量 $.globalEval( "alert(name);" ); // 全局变量 } test(); })
定义和用法
jQuery.globalEval() 函数用于全局性地执行一段JavaScript代码。
提示:该函数的作用与常规的JavaScript eval()函数相似。不同的是,jQuery.globalEval()执行代码的作用域为全局作用域。
语法
$.globalEval( code )
参数 | 描述 |
---|---|
code | String类型 指定的需要运行的JavaScript代码字符串。 |
eval动态操作easyui的表格
var express = ""; //表达式 for (var i = 0; i < cmds.length; i++) { var isNum = $.isNumeric(cmds[i].value); var sValue = isNum ?"(" + cmds[i].value + "*1.00)" : ("(row['" + cmds[i].value + "']*1.00)"); if (i == cmds.length - 1) express += "((row['" + cmds[i].col_id + "']*1.00)" + oper(cmds[i].operator) + sValue + ")"; else express += "((row['" + cmds[i].col_id + "']*1.00)" + oper(cmds[i].operator) + sValue + ")" + oper(cmds[i].operatorx); } for (var j = 0; j < rows.length; j++) { var row = rows[j]; try { var result = eval(express); if (result > 9999999 || result < -99999999) continue;//正,负无穷不处理 var index = that.getRowIndex(row); that.startGridEdit(index); var edCol = that.getCurrnetEditor(col); if (edCol) edCol.target.numberbox("setValue", result); } catch (err) { } } that.stopGridEdit();
//局部函数 function updateRow(row) { that.updateRow(that.getRowIndex(row), row); } var cmd = "row['" + col + "'] = "; var express = ""; //表达式 for (var i = 0; i < cmds.length; i++) { var isNum = $.isNumeric(cmds[i].value); var sValue = isNum ? cmds[i].value : ("row['" + cmds[i].value + "']"); if(i==cmds.length - 1) express += "(row['" + cmds[i].col_id + "']" + oper(cmds[i].operator) + sValue + ")"; else express += "(row['" + cmds[i].col_id + "']" + oper(cmds[i].operator) + sValue + ")" + oper(cmds[i].operatorx); } cmd += express + ";updateRow(row);"; //var colOpt = that.currentGrid.grid('getColumnOption', col); for (var j = 0; j < rows.length; j++) { var row = rows[j]; try { //that.startGridEdit(that.getRowIndex(row)); eval(cmd); } catch (err) { } } */