Added in some changes to class handling and some docs for jQuery.nth().

This commit is contained in:
John Resig 2006-12-29 17:21:48 +00:00
parent 666cc90064
commit 5f4f247df0

47
src/jquery/jquery.js vendored
View file

@ -1235,28 +1235,21 @@ jQuery.extend({
}, },
className: { className: {
add: function(o,c){ add: function( elem, c ){
if (jQuery.className.has(o,c)) return; if ( jQuery.className.has( elem, c ) )
o.className += ( o.className ? " " : "" ) + c; return;
elem.className += ( elem.className ? " " : "" ) + c;
}, },
remove: function(o,c){ remove: function( elem, c ){
if( !c ) { elem.className = c ?
o.className = ""; jQuery.grep( elem.className.split(/\s+/), function(cur){
} else { return jQuery.className.has( c, cur );
var classes = o.className.split(" "); }).join(' ') : "";
for(var i=0; i<classes.length; i++) {
if(classes[i] == c) {
classes.splice(i, 1);
break;
}
}
o.className = classes.join(' ');
}
}, },
has: function(e,a) { has: function( elem, c ){
if ( e.className != undefined ) if ( elem.className != undefined )
e = e.className; elem = elem.className;
return new RegExp("(^|\\s)" + a + "(\\s|$)").test(e); return new RegExp("(^|\\s)" + c + "(\\s|$)").test( elem );
} }
}, },
@ -1407,6 +1400,18 @@ jQuery.extend({
return r; return r;
}, },
/**
* A handy, and fast, way to traverse in a particular direction and find
* a specific element.
*
* @private
* @name $.nth
* @type DOMElement
* @param DOMElement cur The element to search from.
* @param Number|String num The Nth result to match. Can be a number or a string (like 'even' or 'odd').
* @param String dir The direction to move in (pass in something like 'previousSibling' or 'nextSibling').
* @cat DOM/Traversing
*/
nth: function(cur,result,dir){ nth: function(cur,result,dir){
result = result || 1; result = result || 1;
var num = 0; var num = 0;
@ -1463,7 +1468,7 @@ jQuery.extend({
submit: "a.type=='submit'", submit: "a.type=='submit'",
image: "a.type=='image'", image: "a.type=='image'",
reset: "a.type=='reset'", reset: "a.type=='reset'",
button: "a.type=='button'", button: "a.type=='button'||a.nodeName=='BUTTON'",
input: "/input|select|textarea|button/i.test(a.nodeName)" input: "/input|select|textarea|button/i.test(a.nodeName)"
}, },
".": "jQuery.className.has(a,m[2])", ".": "jQuery.className.has(a,m[2])",