getElementByClass可以安全地在浏览器(如getElementById)之间使用吗? [英] Is getElementByClass safe to use across browsers like getElementById?
问题描述
getElementByClass可以安全地在诸如getElementById之类的浏览器中使用?
Is getElementByClass safe to use across browsers like getElementById?
更新
目前正在使用:
Update Currently using:
window.opener.document.getElementById
But I want to reference the object by classname (there will be only 1 element with that classname).
推荐答案
但是我想通过classname引用对象(只有该元素与该类名相同) code> getElementsByClassName 不幸的是不可靠的跨浏览器。根据 Quirksmode 。
getElementsByClassName
is unfortunately not reliable cross-browser. It's currently supported by the most recent versions of Firefox, Opera, Safari, and Chrome, but not at all in Internet Explorer or Konqueror, according to Quirksmode.
如果要跨浏览器使用,您必须提供自己的对于那些不支持它们的浏览器的实现,像PPK的博客中一样:
If you want to use it cross-browser, you'll have to have to provide your own implementation for those browsers that don't support it, like in PPK's blog:
function getElementsByClassName(node,classname) {
if (node.getElementsByClassName)
return node.getElementsByClassName(classname);
else {
// your custom function
}
}
这篇关于getElementByClass可以安全地在浏览器(如getElementById)之间使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!