简体   繁体   中英

Select Element By Class in IE Quirks Mode

I am trying to create a bookmarklet for a page that is rendering in IE in quirks mode. I have no control over the source of the page so telling me to change the doctype will not work.

Anyway, I am trying to get an element by its class but it seems getElementsByClassName and querySelectorAll do not work in quirks mode. Are there any other ways to get this done short of walking the dom?

You can make a utility just for browsers that can't do querySelector- note that this example returns an Array, not a nodelist.

function classReunion(classname, tag, parent){
    var A= [], elements, L, who,
    rx= RegExp(/'\\b'+classname+'\\b');
    tag= tag || '*';
    parent= parent || document;
    elements= parent.getElementsByTagName(tag);
    L= elements.length;
    while(L){
        who= elements[--L];
        if(rx.test(who.className)) A.push(who);
    }
    return A;
}

The old browsers do rely on 'walking' the dom to collect groups of elements by class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM