Jquery工做經常使用實例——回調函數的使用

當動畫 100% 完成後,即調用 Callback 函數。若是咱們沒用callback而是直接用語句,那麼有可能達不到咱們想要的效果,好比我下面的一個實例:javascript

<!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=gb2312" />
<title>test</title>
<script type="text/javascript" src="jquery-1.6.1.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $("#button").click(function(){
            $("p").hide("slow");
            alert("內容已經隱藏了!");
        })
    })
</script>
</head>

<body>
<p>Hello,my name is Adam Li.</p>
<input id="button" type="button" value="Click me to see what happened" />
</body>
</html>
我是想點擊按紐「Click me to see what happened」以後先把p段落裏的全部內容隱藏,再彈出對話框提示用戶。可事實並非這樣,結果是先彈出對話框再隱藏段落裏的內容,顯然這不是咱們要的。html

再看看我從新修改後的:java

<!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=gb2312" />
<title>test</title>
<script type="text/javascript" src="jquery-1.6.1.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $("#button").click(function(){
            $("p").hide("slow",function(){
                alert("內容已經隱藏了!");
            });       
        })
    })
</script>
</head>

<body>
<p>Hello,my name is Adam Li.</p>
<input id="button" type="button" value="Click me to see what happened" />
</body>
</html>
此時我點擊「Click me to see what happened」時將先將內容隱藏,再彈出對話框,目的已達到,這就是使用與不使用回調函數的區別,固然,它的用途還不止於此,更多的還要咱們常用才能知道它的更多用處,好比咱們在用fadeOut()後再執行回調fadeIn(),那麼將淡出再淡入,又好比咱們在使用hide()後再執行回調show(),那麼將先隱藏再顯示等等。jquery

相關文章
相關標籤/搜索