2009-08-09 21 views
-1

我讀過,它是更好的性能,讓jquery使用DIV的ID而不是CLASS下面的代碼使用div類而不是ID,你可以讓它使用ID嗎?如何讓jquery使用ID而不是CLASS?

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 
<script type="text/javascript" > 
$(document).ready(function(){ 
    setTimeout(function(){ 
    $(".flash").fadeOut("slow", function() { 
    $(".flash").remove(); 
     }); }, 2000); 
}); 
</script> 
<div class="flash" id="flash">tessssssssssssssssssssssssssssssssssst</div> 

回答

9
$('#flash').fadeOut... 

docs善於解釋這一點。

它就像在CSS:

'#' 的ID。

'。'' for class

+0

很高興知道,我不知道關於CSS的,謝謝 – JasonDavis 2009-08-09 01:02:30

2

使用哈希(#),而不是一個點(.):

$("#flash").fadeOut("slow", function() { 
$("#flash").remove(); 
... 
1

你也可以使用$(function(){});作爲$(document).ready(function(){})的快捷方式; 是的,你可以使用(#)insted(。)。你也不必要求寫$(「#閃」)再次裏面下面的代碼片段

$("#flash").fadeOut("slow", function() 
    { 
     $("#flash").remove(); 
    }); 

你可以寫

$("#flash").fadeOut("slow", function() 
    { 
     $(this).remove(); 
    }); 

,即你可以用做到這一點關鍵字

嘗試這個例子:

<html> 
<head> 
    <title></title> 

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 
<script type="text/javascript" > 
    $(function() 
    { 
     setTimeout(function() 
     { 
      $("#flash").fadeOut("slow", function() 
      { 
       $(this).remove(); 
      }); 
     }, 2000); 
    }); 
</script> 


</head> 
<body> 
<div class="flash" id="flash">tessssssssssssssssssssssssssssssssssst</div> 

</body> 

</html> 
相關問題