我得到了这个 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。
谁有更好的主意?
最佳答案
这是针对不兼容浏览器 ( citation ) 的 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;
};
}
关于javascript - 按类名选择一个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5149522/