Oggi ho scritto la funzione ToggleClassName perchè la userò in una nuova edizione di Travian Più Più. Serve per aggiungere e togliere una classe CSS ad un elemento, chiaramente se è già presente la toglie, altrimenti la aggiunge.
Io l’ho implementata con Element.prototype chiaramente può essere fatta anche come funzione esterna, con piccoli cambiamenti.
Element.prototype.toggleClassName = function(className) {
if ( (' '+this.className+' ').indexOf(' '+className+' ') > -1) {
var str = ' '+this.className+' ';
str = str.replace(new RegExp(' '+className));
str = str.substr(1).substr(0,str.length-2);
this.className = str;
return false;
} else {
this.className += ' '+className;
return true;
}
}
Il grosso problema che ho dovuto affrontare è nel separatore spazio delle classi, quindi il codice non risulta snellissimo, per quelli interessati si può raggruppare le prime 4 righe dopo il primo if in una unica, ma a me non piace tantissimo e poi diventa una riga troppo lunga per essere visualizzata qui :P
Ultima annotazione, la funzione restituisce true se aggiunge, falsese toglie.
Pingback: feather hair extensions
Pingback: online casino
Pingback: Votes
Pingback: Dallas SEO
Pingback: Nederlands beste hosting support
Pingback: Rain-X
Pingback: Dallas Real Estate Photo
Pingback: Green Travel Northern Thailand
Pingback: Custom iphone covers
Pingback: home based job and business opportunity
Pingback: online shopping from
Pingback: electric dog fence
Pingback: sts
Pingback: free law of attraction
Pingback: meditaciones guiadas
Pingback: treat chronic pain
Pingback: tarot consulta
Pingback: dia transportation
Pingback: Cheap Auto Insurnace in GA
Pingback: Chatting Box
Pingback: ipad 64gb 3g
Pingback: wayfarer
Pingback: Herbal Incense Review
Pingback: how to make extra cash
Pingback: car accident attorney sacramento
Pingback: free slots with bonus
Pingback: Design Trophy
Pingback: www
Pingback: Read more on Herbal Incense
Pingback: mclay boats
Pingback: clairvoyants