jquery.dependClass.js
1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* Depend Class v0.1b : attach class based on first class in list of current element
* File: jquery.dependClass.js
* Copyright (c) 2009 Egor Hmelyoff, hmelyoff@gmail.com
*/
(function($) {
// Init plugin function
$.baseClass = function(obj){
obj = $(obj);
return obj.get(0).className.match(/([^ ]+)/)[1];
};
$.fn.addDependClass = function(className, delimiter){
var options = {
delimiter: delimiter ? delimiter : '-'
}
return this.each(function(){
var baseClass = $.baseClass(this);
if(baseClass)
$(this).addClass(baseClass + options.delimiter + className);
});
};
$.fn.removeDependClass = function(className, delimiter){
var options = {
delimiter: delimiter ? delimiter : '-'
}
return this.each(function(){
var baseClass = $.baseClass(this);
if(baseClass)
$(this).removeClass(baseClass + options.delimiter + className);
});
};
$.fn.toggleDependClass = function(className, delimiter){
var options = {
delimiter: delimiter ? delimiter : '-'
}
return this.each(function(){
var baseClass = $.baseClass(this);
if(baseClass)
if($(this).is("." + baseClass + options.delimiter + className))
$(this).removeClass(baseClass + options.delimiter + className);
else
$(this).addClass(baseClass + options.delimiter + className);
});
};
// end of closure
})(jQuery);