<mySearch ⁄>
<mySnippets order="rand" ⁄>
<myContacts ⁄><email ⁄>
<windows live messenger ⁄>
<myCurriculum type="pdf" ⁄>
<myBlog show="last" ⁄>
<myNews show="rand" ⁄>
<myNews type="cat" ⁄>
<myQuote order="random" ⁄>A maior glória não é ficar de pé, mas levantar-se cada vez que se cai
<myPhoto order="random" ⁄>
<myAdSense ⁄>
<myVisitorsMap ⁄>
body{font-family: Tahoma, Verdana;} .red{color: #B02B2C;} .dark_gray{color: #666;} .orange{color: #FF7400;} .blue {color: #4096EE;}
/** * * jquery.SwitchClass.js - A jQuery Plugin * * @version: 0.1 * @author pedrocorreia.net * @date 2010-04-21 * @projectDescription: Switch/ Replace/ Change an object's class name * @requires jquery.js (tested with 1.3.2) */ (function($){ /** * Switch element(s) Class Name * * @author pedrocorreia.net * * @param Object || Array Selector(s) -> Syntax: [ {elem, old_class, new class}, ... ] */ $.SwitchClass = function (selectors){ var _selectors = [], //our selectors array container, it will hold all our objects /** * Switch Class * * @private * @param Jquery Object * @param String Old Class Name * @param String New Class Name */ _Switch = function(o, oldClass, newClass){ if(o && oldClass && newClass && o.hasClass(oldClass)){ o.removeClass(oldClass).addClass(newClass); } }; //check if we have an object or an array of objects, //the "selectors" will always be in an array, even if we //specify an Object, this will be added to our selectors Array if($.isArray(selectors)){ _selectors = selectors; } else{ _selectors.push(selectors); } //do we have any elements in our, selectors, array? if (_selectors.length === 0) { return; } //iterate over selectors Array $.each(_selectors, function(idx, selector){ _Switch($(selector.elem), selector.old_class, selector.new_class); }); }; /** * Switch element(s) Class Name * jQuery custom function * * @author pedrocorreia.net * * @param String Old Class Name * @param String New Class Name * @extends jQuery */ $.fn.SwitchClass = function(old_class, new_class){ return this.each(function(){ $.SwitchClass({elem: this, old_class: old_class, new_class: new_class}); }); }; })(jQuery);
$(document).ready(function() { //switch class by using an object $.SwitchClass({ elem: "span", old_class: "red", new_class: "dark_gray" } ); //switch class by using an array of objects $.SwitchClass([ { elem: "#my_header", old_class: "red", new_class: "orange" }, { elem: "h4", old_class: "red", new_class: "blue" } ]); //using jQuery selector $("strong, a").SwitchClass("red", "dark_gray"); //using jQuery selector and jQuery chaining $("#my_other_div") .SwitchClass("dark_gray", "red") .find("p:first") .append("... and this text will be added"); });
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <link rel="stylesheet" href="style.css" /> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <script type="text/javascript" src="jquery.SwitchClass.js"></script> <script type="text/javascript" src="Init.js"></script> <title>jQuery SwitchClass</title> </head> <body> <div id="my_div"> <span class="red">This is a span</span>, <strong class="red">this is bold</strong>, <a href="http://www.pedrocorreia.net" class="red">this is a link</a>. <h3 id="my_header" class="red">This is a Header 3</h3> <h4 class="red">This is a Header 4</h4> </div> <div id="my_other_div" class="dark_gray"> <p>This paragraph, initially will be dark gray, but it'll turn to <strong>red</strong>.</p> </div> </body> </html>

