.ready()內部函數做用域問題

使用jQuery的.ready()方法後,內部函數可能出現沒法被外部調用的問題,以下html

 
$(document).ready(function() {
    function Alert() {
     alert("hello");
    };
});

此時Alert()將沒法被button調用;而另外一種狀況以下:jquery

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
</head>

    <button id="btn" onclick="Alert()">彈出</button>

</body>
</html>
$(document).ready(function() {
     var Alert=function() {
     alert("hello");
    };
});

這個時候Alert()就能夠被外部調用了;函數

相關文章
相關標籤/搜索