要素のスタイル属性を変更するだけでなく、新しいスタイルを作成したい。問題を示すコードの例を次に示します。
//Create 1st element
var element1 = $('<div />').text('element1').addClass('blue');
$('body').append(element1);
//Set some css for all elements with the blue class
$('.blue').css('background-color', 'blue');
//Create 2nd element, it won't have the css applied because it wasn't around when the css was set
var element2 = $('<div />').text('element2').addClass('blue');
$('body').append(element2);
上記のコードでは、2 番目の要素は 1 番目と同じスタイルではありません。私が望むのは、将来のすべての要素の className に css を設定できるようにすることです。