按类名选择 div

发布于 2024-10-19 19:09:15 字数 603 浏览 5 评论 0原文

我得到了这个 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>

在我的页面中间,它没有 id,我无法编辑页面 HTML,我也无法使用 jQuery。还尝试用 IE7 和 IE8 来实现。

这里是噩梦:)

解决方案是 document.getElementsByClassName 但这与 ie7 和 ie8 不兼容。

这个div被埋在大约10个div中,所有这些div都是相似的风格,没有id等。这个div上的类是独一无二的!

我能看到的唯一解决方案是获取所有 div 并循环它们以查找类似的 hasAttriutes 。

有人有更好的主意吗?

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 compatible.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

私野 2024-10-26 19:09:15

以下是针对不兼容浏览器的 getElementsByClassName 的跨浏览器实现 (引用):

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;

    };
}

Here's a cross-browser implementation of getElementsByClassName for non-compliant 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;

    };
}
两人的回忆 2024-10-26 19:09:15

不,就是这样完成的。除非它们位于带有 ID 的某物中,否则您将不得不迭代页面上的所有 DIV。幸运的是,它只是一个列表(不需要通过树递归),所以它还不错。

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.

谁的年少不轻狂 2024-10-26 19:09:15

我建议使用 XPaths 来选择节点。可能有用...

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

万劫不复 2024-10-26 19:09:15

使用 jQuery/Sizzle。 IE6及以上版本。 :)

加载它:

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

使用它:

jQuery('.class.otherclass.anotherclass')

Use jQuery/Sizzle. IE6 and up. :)

Load it:

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

Use it:

jQuery('.class.otherclass.anotherclass')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文