eval 将字符串转换成js 代码执行
和你的getElementById完全是两回事
"div"+id是个字符串实例变量
eval("var div=div"+id);div是个js变量符号它可能指向 "div"+id 这个字符串实例引用。
lz这个例子有点特殊,一般不这么写,因为这个只针对IE有效,IE会吧htmlElement的id属性当作变量执行。

解决方案 »

  1.   

    eval
    Evaluates a string of JavaScript code without reference to a particular object.
    核心函数  
    实现版本 Navigator 2.0 语法
    eval(string) 
    参数
    string A string representing a JavaScript expression, statement, or sequence of statements. The expression can include variables and properties of existing objects. 描述
    The argument of the eval function is a string. If the string represents an expression, eval evaluates the expression. If the argument represents one or more JavaScript statements, eval performs the statements. Do not call eval to evaluate an arithmetic expression; JavaScript evaluates arithmetic expressions automatically.
    If you construct an arithmetic expression as a string, you can use eval to evaluate it at a later time. For example, suppose you have a variable x. You can postpone evaluation of an expression involving x by assigning the string value of the expression, say "3 * x + 2", to a variable, and then calling eval at a later point in your script.eval is also a method of all objects. This method is described for the Object class.
    示例
    The following示例 display output using document.write. In server-side JavaScript, you can display the same output by calling the write function instead of using document.write.
    示例 1. Both of the write statements below display 42. The first evaluates the string "x + y + 1"; the second evaluates the string "42".var x = 2
    var y = 39
    var z = "42"
    document.write(eval("x + y + 1"), "<BR>") 
    document.write(eval(z), "<BR>") 示例 2. In the following example, the getFieldName(n) function returns the name of the specified form element as a string. The first statement assigns the string value of the third form element to the variable field. The second statement uses eval to display the value of the form element.var field = getFieldName(3) 
    document.write("The field named ", field, " has value of ", 
       eval(field + ".value")) 示例 3. The following example uses eval to evaluate the string str. This string consists of JavaScript statements that open an Alert dialog box and assign z a value of 42 if x is five, and assigns 0 to z otherwise. When the second statement is executed, eval will cause these statements to be performed, and it will also evaluate the set of statements and return the value that is assigned to z.var str = "if (x == 5) {alert('z is 42'); z = 42;} else z = 0; "
    document.write("<P>z is ", eval(str)) 示例 4. In the following example, the setValue function uses eval to assign the value of the variable newValue to the text field textObject:function setValue (textObject, newValue) {
       eval ("document.forms[0]." + textObject + ".value") = newValue
    } 示例 5. The following example creates breed as a property of the object myDog, and also as a variable. The first write statement uses eval('breed') without specifying an object; the string "breed" is evaluated without regard to any object, and the write method displays "Shepherd", which is the value of the breed variable. The second write statement uses myDog.eval('breed') which specifies the object myDog; the string "breed" is evaluated with regard to the myDog object, and the write method displays "Lab", which is the value of the breed property of the myDog object.function Dog(name,breed,color) {
       this.name=name
       this.breed=breed
       this.color=color
    }
    myDog = new Dog("Gabby")
    myDog.breed="Lab"
    var breed='Shepherd'
    document.write("<P>" + eval('breed'))
    document.write("<BR>" + myDog.eval('breed'))