0

回调函数适用于:

function mySandwich(param1, param2, callback) {  
    alert('Started eating my sandwich.\n\nIt has: ' + param1 + ', ' + param2);  
    callback();  
}  

mySandwich('ham', 'cheese', function() {  
    alert('Finished eating my sandwich.');  
}); 

在体内标签中。

当我尝试时:

<html>
<head>
<script>
    function CallAfterLogin()
    {
        document.mySandwich('hi',function(send){ 
        alert("finished");
        });
    }
</script>
</head>
<body>
<button type="button" onclick="CallAfterLogin()">Click Me!</button> 
<script>
    function mySandwich(param1, callback) {  
        alert('Started eating my sandwich.\n\nIt has: ' + param1 );  
        callback();  
    }  
</script>
</body>
</html>

它给出错误:document.mySandwich 未定义。有人可以告诉我问题出在哪里吗?

4

1 回答 1

2

mySandwich不是附加到文档对象的函数,它是全局范围内的函数,所以document.mySandwich(...)不仅仅是使用mySandwich(...)

function CallAfterLogin() {
    mySandwich('param1', 'param2', function(send){ 
        alert("finished");
    });
}
于 2013-08-12T05:08:24.600 回答