Erstellen Sie eine neue (dauerhafte) CSS in jQuery

Ich möchte erstellen Sie einen neuen Stil, nicht einfach ändern, das style-Attribut eines Elements. Hier einige Beispiel-code, um das problem zu demonstrieren:

//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);​

Im code oben das 2. element nicht der gleiche Stil wie bei der 1. Was ich möchte ist, um in der Lage sein, das css auf eine className für alle zukünftigen Elemente.

JSFiddle

InformationsquelleAutor Drahcir | 2012-09-18
Schreibe einen Kommentar