2013-08-04 11 views
6

voglio creare un foglio di stile come questo:assegnazione non valido lato sinistro con ternario se

var sheet = document.createElement('style'); sheet.type = 'text/css'; 
sheet.innerHTML = data.style; 

Ma it seems che IE ha bisogno la propria sintassi. Per semplificare il codice this answer s', ho cercato

var sheet = document.createElement('style'); sheet.type = 'text/css'; 
(sheet.styleSheet ? sheet.styleSheet.cssText : sheet.innerHTML) = data.style; 

Ma che getta ReferenceError: invalid assignment left-hand side.

Poi, devo usare ...

var sheet = document.createElement('style'); sheet.type = 'text/css'; 
if(sheet.styleSheet) sheet.styleSheet.cssText = data.style; 
else sheet.innerHTML = data.style; 

... o c'è un'alternativa più semplice?

+0

Credo che si possa andare via con l'impostazione di entrambi i valori e che il sistema di oggetti di Javascript sia abbastanza robusto da occuparsi della differenza. – abiessu

+0

@abiessu Ma se 'sheet.styleSheet' è' indefinito', quindi 'sheet.styleSheet.cssText = sheet.innerHTML = data.style' genera' TypeError: sheet.styleSheet è undefined'. Quindi, dovrei anche controllare se dovrei creare l'oggetto 'sheet.styleSheet' – Oriol

+0

No, voglio dire usando due linee di assegnazione separate. È probabile che l'errore di tipo venga generato perché 'sheet.stylesheet' non esiste allo scopo di fornire un valore accessibile a' sheet.styleSheet.cssText' nel tuo esempio. – abiessu

risposta

3

si può sempre fare:

sheet.styleSheet ? sheet.styleSheet.cssText = data.style 
       : sheet.appendChild(document.createTextNode(data.style)); 

FIDDLE

+0

bene ora che è ancora meno leggibile ... – Dave

+0

@Dave - ma risponde alla domanda! – adeneo

+0

Beh, volevo evitare di ripetere 'data.style', ma credo che questo è il codice di lavoro più semplificata – Oriol

1

ecco la soluzione che non lascia manutentori futuri chiedendo cosa diavolo fa il codice:

function setSheetContent(sheet, text) { 
    if(sheet.styleSheet) 
     sheet.styleSheet.cssText = text; 
    else 
     sheet.innerHTML = text; 
} 

var sheet = document.createElement('style'); 
sheet.type = 'text/css'; 
setSheetContent(sheet, data.style); 

o avvolgere in su per una comodità ancora maggiore (se non si desidera modificare il contenuto di un foglio esistente)

function stylesheetWithContent(sheet, text) { 
    var sheet = document.createElement('style'); 
    sheet.type = 'text/css'; 
    if(sheet.styleSheet) 
     sheet.styleSheet.cssText = text; 
    else 
     sheet.innerHTML = text; 
    return sheet; 
} 

var sheet = stylesheetWithContent(data.style); 
+1

Inoltre non mettere molte affermazioni su una riga. Se si desidera minimizzare il codice per download più veloci, utilizzare un minificatore effettivo (ad esempio, il compilatore di chiusura di Google ha una versione online) – Dave

+0

Ho usato solo 'var sheet = document.createElement ('style'); sheet.type = 'text/css'; 'sulla stessa riga perché quando scrivo HTML uso'