我得到了这个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中,所有这些都是类似的风格,没有id等等。这个div上的类是独一无二的!
我能看到的唯一解决方案是获取所有div并循环查找hasAttriutes类似。
任何人都有更好的主意吗?
答案 0 :(得分:6)
以下是针对不合规浏览器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;
};
}
的跨浏览器实现:
{{1}}
答案 1 :(得分:0)
不,这就是它的完成方式。除非他们在带有ID的某事中,否则你会在页面上迭代所有DIV。幸运的是,它只是一个列表(不需要通过树递归)所以它不是那么糟糕。
答案 2 :(得分:0)
我建议使用XPath来选择节点。可能会工作......
答案 3 :(得分:0)
使用jQuery / Sizzle。 IE6及以上。 :)
加载它:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
使用它:
jQuery('.class.otherclass.anotherclass')