VK wrote:[color=blue]
> Wim Roffil wrote:
>[color=green]
>>I have a simple webpage (without stylesheet) where I want to be able to
>>switch some parts of the page off. I tried to do this by giving all the
>>elements that should be switched off the class "dname" and then to call the
>>javascript
:[/color]
>
>
> Surprisingly enough DOM still doesn't have
> document.getElementsByClassName() method. It is one of misteries of
> academical thinking I guess. :-)
>
> prototype.js library has an emulation of this method. It is totally
> free to use and destribute and it contains nothing really proprietary
> in it. But as I'm not the first one who wrote this particular piece of
> code, I feel obligated to place the MIT licence notice (so don't get
> scared ;-)
>
> (c) 2005 Sam Stephenson
>
http://prototype.conio.net/
>
http://www.opensource.org/licenses/mit-license.php
>
> document.getElementsByClassName = function(className) {
> var children = document.getElementsByTagName('*') || document.all;
> var elements = new Array();
>
> for (var i = 0; i < children.length; i++) {
> var child = children[i];
> var classNames = child.className.split(' ');
> for (var j = 0; j < classNames.length; j++) {
> if (classNames[j] == className) {
> elements.push(child);
> break;
> }
> }
> }
> return elements;
> }
>[/color]
I seems to me if you are going to that much trouble, it should create an
object that has all the class names as properties and the value of each
should be an array of references to the elements that have that class.
It's not dynamic, so any elements added to the document would have to be
added to the appropriate properties array (or a new one added).
It would be a kind of classes collection.
e.g.:
// Initiallise with a property for elements with no class
var ClassObj = { noClass:[] };
function loadClassObj(obj)
{
var el, els=obj.childNodes, i=els.length;
var eClass, eClasses, j;
while (i--){
el = els[i];
if (el.className){
eClasses = el.className.split(' ');
j=eClasses.length;
while (j--){
eClass = eClasses[j];
if (ClassObj[eClass]){
ClassObj[eClass][ClassObj[eClass].length] = el;
} else {
ClassObj[eClass] = [el];
}
}
} else {
ClassObj.noClass[ClassObj.noClass.length] = el;
}
if (el.childNodes){
loadClassObj(el)
}
}
}
window.onload = function (){
var docBody = document.body || document.documentElement;
loadClassObj(docBody);
}
Lightly tested in Firefox & IE, it should work in nearly all browsers,
but care needs to be taken with class attribute values - only single
spaces between class names and none before and after (that could be
fixed with a regExp, but speed may suffer even more). The loadClassObj
function could be made a method of the ClassObj.
But I think it would be slow for a big document, a much faster way is to
modify the class rule directly, but support in various browsers may not
be sufficient for the OP's purpose. There is a post here:
<URL:http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/41fc5fcdf69a934/eeb33edb6af79855?q=document.styleSheets&rnum=13&hl =en#eeb33edb6af79855>
A modified version here:
<URL:http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3daffc9ed1c3afee/f800fd5529ed50b0?q=modify+class+rule+style&rnum=1# f800fd5529ed50b0>
Some compatibility information is here:
<URL:http://www.quirksmode.org/dom/w3c_css.html>
--
Rob