2011-07-21 36 views
1
<head> 
    <title>Untitled Page</title> 
    <script src="jquery-1.4.1.js"></script> 
    <script type="text/jscript"> 
     function changecolor() 
     { 


      $('li').each(function() 
      { 
       var sval = $(this).text(); 
       //alert(sval); 
       if (sval == "Rose") 
       { 
        alert(sval); 
        $(item).css('backgroundcolor', 'red'); 
       } 

      }); 
     } 
    </script> 
</head> 
<body> 
    Select your fraglance : 
    <ul> 
     <li>Rose</li> 
     <li>White Jasmine</li> 
     <li>Yellow Sunflower</li> 
     <li>Pink Lotus</li> 
     <li>White Daisy</li> 
    </ul> 
    <button id="text" onclick="changecolor()">Change Color</button> 
</body> 
</html> 

回答

0

你可能想要使用background-color,因爲這是CSS名稱。

$(item).css('background-color', 'red'); 
0

凡在JS中定義的項目?可能是它的一個錯誤。試試這個

<script type="text/jscript"> 
     function changecolor() 
     { 


      $('li').each(function() 
      { 
       var sval = $(this).text(); 
       //alert(sval); 
       if (sval == "Rose") 
       { 
        alert(sval); 
        $(this).css('backgroundcolor', 'red'); 
       } 

      }); 
     } 
    </script> 
0

我想你忘記你的。每次內部項目

$('li').each(function(i, item)