2012-09-08 31 views
0

我有一個AJAX函數來隱藏和顯示HTML中表單的字段。我想在AJAX函數中添加一些CSS。在AJAX函數中添加CSS

<script> 
    $(function() { 

     $('#advo_other').hide(); 
     $('#advocate').change(function() { 

      var val = $(this).val(); 

      $('#advo_other').hide(); 

      switch (val) { 
       case 'Other': 
        $('#advocate') // want to add css there...... 
        $('#advo_other').show(); 
        break; 
      } 
     }); 
    }); 
</script> 

和風格低於:

<style type="text/css"> 
    .fade { 
     color: #CCC; 
     border-color:#CCC; 
    } 
</style> 
+0

第一步應該看看jQuery文檔:http://api.jquery.com/category/css/。 –

+0

** OMG o_O我不知道爲什麼..但它與ajax無關.. O_o ** –

回答

1

使用addClass函數。

所以,你將有:

<script> 
    $(function() { 

     $('#advo_other').hide(); 
     $('#advocate').change(function() { 

      var val = $(this).val(); 

      $('#advo_other').hide(); 

      switch (val) { 
       case 'Other': 
        $('#advocate').addClass("fade"); 
        $('#advo_other').show(); 
        break; 
      } 
     }); 
    }); 
</script> 
1

您可以添加CSS with Jquery到任何這樣的元素:

$('#advocate').css('color', '#005DAA'); 

或者你可以用樣式規則一類添加到元素

$('#advocate').addClass('fade'); 
0

嘗試;

$(function() { 
    $('#advo_other').hide(); 
    $('#advocate').change(function() { 
    var val = $(this).val(); 
    $('#advo_other').hide(); 
    switch (val){ 
     case 'Other': 
     $('#advocate').addClass("fade"); 
     $('#advo_other').show(); 
     break; 
    } 
    }); 
}); 

$('#advocate')用於選擇id爲advocate的元件。你可以在你的CSS中創建風格元素。使用addClass("class_name")爲您的元素添加一些類樣式,並使用.removeClass("class_name")刪除類樣式。