Javascript document.getElementsByTagName("*") 或 document.all
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2095966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
document.getElementsByTagName("*") Or document.all
提问by Jason
document.getElementsByTagName("*")works for IE/Firefox/Opera, But doesn't work for Chrome and Safari.
document.getElementsByTagName("*")适用于 IE/Firefox/Opera,但不适用于 Chrome 和 Safari。
document.allworks for IE/Chrom/Safari, But doesn't work for Firefox.
document.all适用于 IE/Chrom/Safari,但不适用于 Firefox。
How can I deal with it?
我该如何处理?
采纳答案by Khurram Hassan
document.allshould be avoided as it is not Standards compliant. Instead you can use document.getElementById()for Particular Node or use
$("*")For selecting all the elements using jQuery.
document.all应该避免,因为它不符合标准。相反,您可以使用document.getElementById()特定节点或使用
$("*")jQuery 选择所有元素。
But still if you would like to use document.all then see to it that <!DOCTYPE>Tag is removed from your page, as well as the xmlns attribute is also removed from your <html>tag.
但是,如果您想使用 document.all,那么请确保<!DOCTYPE>从您的页面中删除了Tag,并且也从您的<html>标签中删除了 xmlns 属性。
Change anything like this:
像这样改变任何东西:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
to:
到:
<html>
I have tested it on FireFox 19.0.2 and document.allworks fine for me.
我已经在 FireFox 19.0.2 上对其进行了测试,对document.all我来说效果很好。
The reason:When you use the <!DOCTYPE>tag you are telling the browser that your webpage is complaint with the standards which tells you to NOT use the document.allin your script, so the browser also does not allow it.
原因:当您使用<!DOCTYPE>标签时,您是在告诉浏览器您的网页不符合标准,该标准告诉您不要document.all在脚本中使用 ,因此浏览器也不允许这样做。
But as you want to use it, you are obviously not following the standards so don't even bother to add the <!DOCTYPE>tag otherwise document.allwon't work.
但是当你想使用它时,你显然没有遵循标准,所以甚至不要费心添加<!DOCTYPE>标签,否则document.all将无法工作。
回答by kjagiello
Try like this:
像这样尝试:
if (document.all !== undefined)
{
allElements = document.all;
}
else
{
allElements = document.getElementsByTagName("*");
}
Or shorter version
或者更短的版本
allElements = document.all ? document.all : document.getElementsByTagName("*");
回答by Kornel
document.getElementsByTagName()works perfectly in all modern browsers (everything newer than IE5).
document.getElementsByTagName()在所有现代浏览器中都能完美运行(比 IE5 新的所有浏览器)。
If it doesn't seem to work in Chrome or Safari, then it's most likely just a symptom of an error you have elsewhere.
如果它在 Chrome 或 Safari 中似乎不起作用,那么它很可能只是您在其他地方遇到的错误的症状。
回答by Platinum Azure
I admit that there might be technologies today I don't know about that allow for this sort of thing to be done in cross-browser format, but the way I've always had to do it in the past is to have a check of some sort for which browser you're using.
我承认今天可能有一些我不知道的技术允许以跨浏览器格式完成这类事情,但我过去一直必须这样做的方式是检查某种您正在使用的浏览器。
A simpler solution, though, is to try to run one of them, and if you get nothing/null/an error, use the other.
不过,一个更简单的解决方案是尝试运行其中一个,如果您什么也没有得到/空/错误,请使用另一个。
Anyway, if you really don't want to deal with it yourself, you should use a library that will deal with it for you (e.g., jQuery).
无论如何,如果你真的不想自己处理它,你应该使用一个可以为你处理它的库(例如,jQuery)。
回答by Brandon Olson
Without sufficient rep to comment on @Khurram Hassan 's answer, I'll put it here, along with my answer to the original question.
如果没有足够的代表对@Khurram Hassan 的回答发表评论,我会把它和我对原始问题的回答一起放在这里。
First, the original question. My solution would be (and is in my own code, pending an answer to a problem with it which I put up on this site) document.getElementsByTagName("*"), which actually does get every single element on Chrome. I tested it out on google.com on Google Chrome with a profile loaded and the eight most common visited sites listed, and it came out to 356 individual elements with a tag name. In fairness, this included html, head, body, and others that probably aren't useful, but it still got them. I don't have access to Opera at the moment, but with Chrome still accepting that piece of JavaScript, I don't see a reason why it wouldn't accept that in your code.
首先,原始问题。我的解决方案是(并且在我自己的代码中,等待我在本网站上提出的问题的答案)document.getElementsByTagName("*"),它实际上确实获取了 Chrome 上的每个元素。我在谷歌浏览器上的 google.com 上测试了它,加载了一个配置文件并列出了八个最常访问的站点,它有 356 个带有标签名称的单个元素。公平地说,这包括 html、head、body 和其他可能没有用的东西,但它仍然得到了它们。我目前无法访问 Opera,但由于 Chrome 仍然接受那段 JavaScript,我看不出它在您的代码中不接受的原因。
Second, for @Khurram hassan, document.getElementById()can't be used in this case. I just tested it on Chrome, and it came up with a value of null. In theory, anything with the general form of getElementsBy*as opposed to getElementBy*could probably be used in this case. So, to add to my earlier answer, you might also try ClassName, Name, and TagNameNSdepending on what you're trying to do. On the same page as before, I tested those three and while only TagNameNSworked, the rest just returned empty lists, not errors.
其次,对于@Khurram hassan,document.getElementById()在这种情况下不能使用。我刚刚在 Chrome 上对其进行了测试,它得出的值为 null。从理论上讲,与一般形式的东西getElementsBy*来,而不是getElementBy*也许可以在这种情况下使用。因此,要添加到我之前的答案中,您还可以尝试ClassName, Name, 并TagNameNS取决于您要做什么。在与以前相同的页面上,我测试了这三个,虽然只TagNameNS工作,其余的只是返回空列表,而不是错误。
Further, if it is true that you don't need <!DOCTYPE html>in your code, perhaps you could post the working code as an edit to your answer so that we can see it. <!DOCTYPE html>seems, to the best of my knowledge, to be the commonly accepted (and usually assumed to be mandatory) way of starting an HTML page. If it's not necessary, then that would be new information (at least to me) that could prove useful in either debugging or non-public webpages down the road.
此外,如果<!DOCTYPE html>您的代码中确实不需要,也许您可以将工作代码作为对答案的编辑发布,以便我们可以看到它。<!DOCTYPE html>据我所知,似乎是开始 HTML 页面的普遍接受(并且通常被认为是强制性的)方式。如果没有必要,那么这将是新信息(至少对我而言),可以证明对调试或非公共网页有用。
回答by Chris Love
While I would not encourage you to do a document.all because it implies you are doing a lot of client-side parsing that is just not needed I do understand there is a lot of legacy stuff out there etc. I wanted to post a little extension of my thoughts on a wrapper method for document.all.
虽然我不鼓励你做一个 document.all ,因为它意味着你正在做很多不需要的客户端解析,但我知道那里有很多遗留的东西等等。我想发布一点扩展我对 document.all 的包装方法的想法。
document.all = document.all || function(){
return document.getElementsByTagName("*");
};
Of course this assumes you have a getElementsByTagName function, which should be the case.
当然,这假设您有一个 getElementsByTagName 函数,应该是这种情况。

