最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Selecting a div by classname - Stack Overflow

programmeradmin4浏览0评论

I got this div...

<div tabindex="0" class="button-base inline-block button aw-btn button-base-active">
    <input type="text" tabindex="-1" style="opacity: 0; height: 1px; width: 1px; z-index: -1; overflow-x: hidden; overflow-y: hidden; position: absolute; ">
 </div>

in the middle of my page, it has no id and I am unable to edit the pages HTML, I am also no able to use jQuery. Also trying to do it with IE7 and IE8.

Nightmare here :)

The solution would be document.getElementsByClassName but that is not ie7 and ie8 patible.

This div is buried in about 10 divs, all of which are similar style with no id's etc. The classes on this div are unique!

The only solution I can see is to get ALL divs and loop them looking for hasAttriutes similar.

Anyone have a better idea?

I got this div...

<div tabindex="0" class="button-base inline-block button aw-btn button-base-active">
    <input type="text" tabindex="-1" style="opacity: 0; height: 1px; width: 1px; z-index: -1; overflow-x: hidden; overflow-y: hidden; position: absolute; ">
 </div>

in the middle of my page, it has no id and I am unable to edit the pages HTML, I am also no able to use jQuery. Also trying to do it with IE7 and IE8.

Nightmare here :)

The solution would be document.getElementsByClassName but that is not ie7 and ie8 patible.

This div is buried in about 10 divs, all of which are similar style with no id's etc. The classes on this div are unique!

The only solution I can see is to get ALL divs and loop them looking for hasAttriutes similar.

Anyone have a better idea?

Share Improve this question asked Mar 1, 2011 at 1:20 WizzardWizzard 12.7k23 gold badges72 silver badges101 bronze badges 4
  • Any chance that input is in a form? – kojiro Commented Mar 1, 2011 at 1:23
  • I hate when people make a hundred "utility" classes and then make divs with a jillion classes. I can understand two or three in some cases... – The Muffin Man Commented Mar 1, 2011 at 1:23
  • check out this link, the poster created a getElementsByClassName() for IE at devshed – Patrick Commented Mar 1, 2011 at 1:23
  • Y'know, if you can edit the JavaScript that runs on the page, you can load up jQuery for yourself using just JavaScript, and then not have to worry about cross-browser issues. jsfiddle/mattball/qsAYE – Matt Ball Commented Mar 1, 2011 at 3:21
Add a ment  | 

4 Answers 4

Reset to default 6

Here's a cross-browser implementation of getElementsByClassName for non-pliant browsers (citation):

if (!document.getElementsByClassName)
{

    document.getElementsByClassName = function(classname)
    {
        var elArray = [];

        var tmp = document.getElementsByTagName("*");

        var regex = new RegExp("(^|\\s)" + classname + "(\\s|$)");
        for ( var i = 0; i < tmp.length; i++ ) {

            if ( regex.test(tmp[i].className) ) {
                elArray.push(tmp[i]);
            }
        }

        return elArray;

    };
}

Nope, that's how it's done. Unless they're in something with an ID you're stuck iterating all DIVs on the page. Fortunately it is just a list though (no need to recurse through a tree) so it's not so bad.

I would suggest using XPaths to select the nodes. Might work...

Use jQuery/Sizzle. IE6 and up. :)

Load it:

<script src="https://ajax.googleapis./ajax/libs/jquery/1.5.1/jquery.min.js"></script>

Use it:

jQuery('.class.otherclass.anotherclass')
发布评论

评论列表(0)

  1. 暂无评论