2015-07-21 77 views
0

我有下面的代碼中,我試圖遮擋效果應用到一個段落,但它不發生的事情:jQuery UI的遮擋效果不工作

<html> 
    <head> 
     <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
     <script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script> 
     <script> 
     $('#but1').click(function(){ 
      $('p').toggle('blind'); 
     }); 
     </script> 
    </head> 
    <body> 
     <button id="but1">Click Me!</button> 
     <p>This is a paragraph.</p> 
    </body> 
</html> 

任何人都可以指出我究竟做錯了什麼?

回答

3

要麼使用

$(document).ready(function() { 
    $('#but1').click(function() { 
     $('p').toggle('blind'); 
    }); 
}); 

<html> 
    <head> 
     <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
     <script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script> 
    </head> 
    <body> 
     <button id="but1">Click Me!</button> 
     <p>This is a paragraph.</p> 
     <script> 
     $('#but1').click(function(){ 
      $('p').toggle('blind'); 

     }); 
     </script> 
    </body> 
</html> 
之前把你的腳本在前端,即
1

將代碼包裝在ready中。

在您的代碼中,click事件未被綁定,因爲#but1元素在執行時不可用於DOM

$(document).ready(function() { 
    $('#but1').click(function() { 
     $('p').toggle('blind'); 
    }); 
});