按类名选择一个 div [英] Selecting a div by classname
问题描述
我得到了这个 div...
在我的页面中间,它没有 id,我无法编辑页面 HTML,我也无法使用 jQuery.也在尝试用 IE7 和 IE8 来做.
噩梦在这里:)
解决方案是 document.getElementsByClassName 但这与 ie7 和 ie8 不兼容.
这个div被埋在大约10个div中,都是类似的风格,没有id等.这个div上的类是独一无二的!
我能看到的唯一解决方案是获取所有 div 并循环它们以寻找类似的 hasAttriutes.
有人有更好的主意吗?
这里是 getElementsByClassName
的跨浏览器实现,适用于不兼容的浏览器 (引用):
if (!document.getElementsByClassName){document.getElementsByClassName = 函数(类名){var elArray = [];var tmp = document.getElementsByTagName("*");var regex = new RegExp("(^|\s)" + 类名 + "(\s|$)");for ( var i = 0; i < tmp.length; i++ ) {如果(regex.test(tmp[i].className)){elArray.push(tmp[i]);}}返回 elArray;};}
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?
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;
};
}
这篇关于按类名选择一个 div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!