W3CSchool定義:
eval() 函數可計算某個字符串,並執行其中的的 JavaScript 代碼。其中,string 必需。要計算的字符串,其中含有要計算的 JavaScript 表達式或要執行的語句。
乍看“計算”,覺得很抽象,自己理解成了執行,執行一段封裝爲字符串的JavaScript函數或者表達式。
所用到的實例是:用jquery獲取到了radio的onclick中的事件——
var radioOnClick = $("input:radio[name="+ radioButtonName + "]").<span style="background-color: rgb(255, 255, 0);">attr("onclick");</span>
然後直接使用eval(radioOnClick);在外面執行了該事件。
此外,另一實例:
<script type="text/javascript">
eval("x=10;y=20;document.write(x*y)")
document.write(eval("2+2"))
var x=10
document.write(eval(x+17))
</script>
運行結果:
200
4
27
W3CSchool 鏈接:http://www.w3school.com.cn/jsref/jsref_evaasp