eval(obj) ; 該函數會將 obj 當做代碼去執行一遍。
例子:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="jquery-1.2.6.js" ></script>
<title> eval 函數的使用 </title>
</head>
<script>
function but1_click(){
var obj = $("#test");
alert(obj.val());
eval (obj.val());//會將 obj.val() 當成代碼去執行
}
</script>
<body>
<textarea id="test" cols="100" rows="10" >
var a = 3;
var b = 5;
var c = a + b;
alert(c);
</textarea>
<input type="button" id="but1" value="執行" onclick="but1_click()" />
</body>
</html>