<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<title></title>
<style type="text/css">
</style>
<p id="a" class="ab cd ef gh ij">abc</p>
<script type="text/javascript">
function TokenList (e, type) {
this.e = e;
this.at = type;
}
TokenList.prototype.contains = (function () {
return function ( name ) {
var cnt, key, keys, str;
str = '\u0020' + this.e[ this.at ] + '\u0020';
if ( -1 < str.indexOf( '\u0020' + name + '\u0020' ) ) {
return true;
}
cnt = 0;
keys = name.split( /\u0020+/ );
while( key = keys[ cnt++ ] ) {
if( -1 == str.indexOf( '\u0020' + key + '\u0020' ) ) {
return false;
}
}
return true;
};
})();
TokenList.prototype.add = (function () {
return function ( name ) {
var cnt, key, keys, str, buf = [ ];
keys = name.split( /\u0020+/ );
str = '\u0020' + this.e[ this.at ] + '\u0020';
cnt = 0;
while( key = keys[ cnt++ ] ) {
if( 0 > str.indexOf( '\u0020' + key + '\u0020' ) ) {
buf.push( key );
}
}
this.e[ this.at ] += '\u0020' + buf.join( '\u0020' );
return buf;
};
})();
TokenList.prototype.item = (function () {
return function ( num ) {
var item = this.e[ this.at ].split( /\u0020+/ );
return item ? item[ num ]: null;
};
})();
TokenList.prototype.length = (function () {
return function ( ) {
return this.e[ this.at ].split( /\u0020+/ ).length;
};
})();
TokenList.prototype.remove = (function () {
return function ( name ) {
var key, keys, str, cnt = 0, buf = [ ];
keys = this.e[ this.at ].split( /\u0020+/ );
str = '\u0020' + name + '\u0020';
while( key = keys[ cnt++ ] ) {
if( 0 > str.indexOf( '\u0020' + key + '\u0020' ) ) {
buf.push( key );
}
}
this.e[ this.at ] = buf.join( '\u0020' );
return buf;
};
})();
TokenList.prototype.toggle = (function () {
return function ( name ) {
var key, keys, str, cnt = 0, buf = [ ];
keys = name.split( /\u0020+/ );
str = '\u0020' + this.e[ this.at ] + '\u0020';
while( key = keys[ cnt++ ] ) {
if ( 0 > str.indexOf( '\u0020' + key + '\u0020' ) ) {
str += key + '\u0020';
} else {
buf.push( key );
}
}
this.e[ this.at ] = str;
if( buf.length ) this.remove( buf.join( '\u0020' ) );
return this.e[ this.at ];
};
})();
var a = { 'classList': new TokenList( document.getElementById('a'), 'className') };
alert(document.getElementById('a').className);
a.classList.toggle( 'cd gh' );
alert(document.getElementById('a').className);
a.classList.toggle( 'cd gh' );
alert(document.getElementById('a').className);
</script>