为什么 document.all 有时像一个对象,有时又不像一个对象

为什么 document.all 有时像一个对象,有时又不像一个对象?
document.all 奇怪的地方在于它是一个数组(其实是对象),但是类型却是 undefined。


>>> document.all
HTMLAllCollection[1009]  // 控制台打印出一个数组
>>> document.all[0]
<html>...</html> // 第一个元素是 html 标签,看起来很像一个数组
>>> typeof document.all
'undefined' // 咦,居然不是 object?
>>> if(document.all){console.log('document.all 为真')}else{console.log('document.all 为假')}
document.all 为假 

我第一次看到 document.all 的时候也很惊讶,怎么会有这么奇葩的对象。


  1. 所有浏览器都支持用 document.all[index] 或者 document.all[id] 获取标签
  2. 但是 typeof document.all,在 IE 10 以下的值为 ‘object’,在其他浏览器的值均为 ‘undefined’


那个时候,浏览器刚刚出现,很多功能不完善,比如 document.getElementById,都有很多浏览器不支持。甚至连 W3C 都还没制定出 Web 标准,这个时候 IE 4(我擦我都没见过 IE 4)推出了一些 API,只有 IE 4 支持,其中就包括我们今天说 document.all,它比 document.getElementById 要好用一些,比如你可以用 document.all[‘topbar’] 来获取元素,所以那个时候很多程序员会些这样的代码:

  // IE 4 代码
}else if(document.getElementById){
  // 其他浏览器代码

甚至某些程序员直接认为 document.all 为真的浏览器,就是 IE。也就是这样:

   // IE
   // 非 IE

其他浏览器(网景)觉得 IE 4 有的功能,我也要有才行。于是也加上 document.all,功能一样,可以获取元素。

但是呢,其他浏览器又不想被某些程序员认为是 IE,于是 typeof document.all 的值定为 undefined。

有人说 W3C 标准出来后,为什么浏览器不把 document.all 纠正过来,去掉或者变成一个正常的对象都可以啊。

太天真了,如果浏览器这么做了,那么当时世界上会有很多页面 JS 代码都不能正常运行,到时候用户只会怪浏览器不给力,然后卸载掉浏览器。


于是,最终,我们在所有浏览器上得到了一个类型为 undefined 的 document.all,只有 IE 10 以下,然后保留着 IE 4 的行为。

不过 document.all 现在已经被弃用了,大家不要再使用它了。用 document.getElementById 或者 document.querySelector 就好 🙂

本文只是讲 document.all 吗?不。

现在的前端新人,对浏览器的演化过程并没有任何体会,以为浏览器天生就是按照 Web 标准实现的。发现 IE 不兼容就指责 IE。




144 thoughts on “为什么 document.all 有时像一个对象,有时又不像一个对象”

  1. Do you have a spam problem on this blog; I also am a blogger, and I was curious about your situation; we have developed some nice practices and we are looking to exchange solutions with others, be sure to shoot me an e-mail if interested.|

  2. Hi! Do you know if they make any plugins to safeguard against hackers? I’m kinda paranoid about losing everything I’ve worked hard on. Any suggestions?|

  3. With havin so much content and articles do you ever run into any issues of plagorism or copyright infringement? My site has a lot of unique content I’ve either written myself or outsourced but it appears a lot of it is popping it up all over the internet without my authorization. Do you know any methods to help prevent content from being stolen? I’d truly appreciate it.|

  4. I’m really enjoying the theme/design of your web site. Do you ever run into any browser compatibility issues? A couple of my blog visitors have complained about my site not operating correctly in Explorer but looks great in Safari. Do you have any advice to help fix this problem?|

  5. obviously like your web site but you have to test the spelling on quite a few of your posts. Many of them are rife with spelling issues and I find it very troublesome to inform the truth nevertheless I’ll definitely come again again.|

  6. I think this is among the most important info for me. And i’m glad reading your article. But should remark on few general things, The website style is great, the articles is really nice : D. Good job, cheers|

  7. you’re truly a excellent webmaster. The website loading pace is amazing. It kind of feels that you’re doing any unique trick. Also, The contents are masterwork. you have done a great job on this subject!|

  8. You really make it seem really easy along with your presentation but I to find this topic to be actually something which I believe I would by no means understand. It kind of feels too complex and extremely extensive for me. I am having a look ahead for your subsequent publish, I will attempt to get the dangle of it!|

  9. Thanks , I’ve recently been searching for information approximately this topic for a while and yours is the greatest I’ve discovered so far. But, what concerning the conclusion? Are you positive concerning the source?|

  10. I blog quite often and I truly thank you for your information. The article has truly peaked my interest. I am going to take a note of your site and keep checking for new information about once per week. I opted in for your RSS feed too.|

  11. Link exchange is nothing else except it is only placing the other person’s webpage link on your page at appropriate place and other person will also do same in support of you.|

  12. Hello all, here every one is sharing these know-how, therefore it’s good to read this blog, and I used to pay a quick visit this weblog every day.|

  13. I don’t even understand how I finished up right here, however I assumed this submit was once good. I don’t recognise who you might be but certainly you are going to a well-known blogger if you are not already. Cheers!|

  14. Hello There. I found your blog using msn. This is a really well written article. I’ll make sure to bookmark it and come back to read more of your useful information. Thanks for the post. I will certainly comeback.|

  15. Hello There. I found your weblog the usage of msn. This is a really smartly written article. I will make sure to bookmark it and return to read extra of your helpful information. Thanks for the post. I’ll certainly return.|

  16. Thank you a bunch for sharing this with all of us you actually understand what you’re talking about! Bookmarked. Please also discuss with my web site =). We can have a link alternate contract among us|

  17. I’ve been surfing online more than 4 hours today, yet I never found any interesting article like yours. It is pretty worth enough for me. Personally, if all website owners and bloggers made good content as you did, the internet will be much more useful than ever before.|

  18. I do not know whether it’s just me or if perhaps everybody else experiencing problems with your site. It seems like some of the text on your posts are running off the screen. Can someone else please comment and let me know if this is happening to them as well? This might be a problem with my browser because I’ve had this happen previously. Cheers|

  19. I like the valuable information you provide in your articles. I’ll bookmark your weblog and take a look at again right here regularly. I’m slightly certain I’ll learn lots of new stuff proper here! Good luck for the following!|

  20. A motivating discussion is worth comment. I do believe that you should publish more about this subject, it may not be a taboo subject but generally folks don’t discuss such issues. To the next! All the best!!|

  21. I like the helpful info you supply in your articles.
    I’ll bookmark your weblog and test once more here frequently.
    I’m rather sure I’ll be told many new stuff right here!
    Good luck for the following!

  22. Neat blog! Is your theme custom made or did you download it
    from somewhere? A design like yours with a few simple tweeks would really make my blog shine.
    Please let me know where you got your theme.
    Thank you

  23. Whats up are using WordPress for your site platform? I’m new to the blog
    world but I’m trying to get started and
    create my own. Do you require any html coding knowledge to make your
    own blog? Any help would be greatly appreciated!

  24. Hello there, I found your web site via Google while
    looking for a related topic, your website came up, it appears to be
    like good. I’ve bookmarked it in my google bookmarks.
    Hello there, just was alert to your weblog via Google, and found that it is
    truly informative. I am going to be careful for brussels.

    I will appreciate when you proceed this in future.

    Numerous other folks will likely be benefited from your writing.

  25. I was suggested this blog by my cousin. I’m not sure whether this post is written by him as nobody else know such detailed about my problem. You’re incredible! Thanks!|

  26. It’s really very difficult in this busy life to listen news
    on Television, so I simply use the web for that reason, and take the latest information.

  27. I am not certain where you are getting your info, but good topic.
    I needs to spend some time learning much more or working out
    more. Thanks for excellent information I used to be on the lookout
    for this information for my mission.

  28. Woah! I’m really enjoying the template/theme of this blog.
    It’s simple, yet effective. A lot of times it’s challenging to get that “perfect balance” between usability
    and appearance. I must say that you’ve done a awesome job with
    this. Also, the blog loads super fast for me on Chrome.
    Superb Blog!

  29. Leading Online Pharmacy – Contact us at +1 (917) 259-3352 for unbelievable rates, discount and offers on any medicine. Get it delivered free of cost at your door steps, call us today. Phone : +1 (917) 259-3352

  30. Hi! I could have sworn I’ve visited your blog before but after browsing through some of the articles I realized it’s new to me. Anyways, I’m certainly happy I stumbled upon it and I’ll be book-marking it and checking back regularly!

  31. Leading Digital Marketing agency in India and Kolkata, Leading Web Design company. Providing web designing and Digital marketing services, Digital marketing course, and certification with an internship.

  32. Having read this I believed it was rather enlightening. I appreciate you finding the time and effort to put this content together. I once again find myself spending a significant amount of time both reading and leaving comments. But so what, it was still worth it!

  33. Your style is so unique in comparison to other folks I have read stuff from. I appreciate you for posting when you’ve got the opportunity, Guess I will just book mark this site.

  34. Spot on with this write-up, I really believe that this website needs a lot more attention. I’ll probably be back again to read through more, thanks for the information!

  35. I’m the business owner of JustCBD Store label (justcbdstore.com) and am trying to develop my wholesale side of business. I am hoping anybody at targetdomain give me some advice ! I considered that the most effective way to accomplish this would be to connect to vape stores and cbd stores. I was really hoping if anybody could suggest a trustworthy web-site where I can buy Vape Shop Contact Details I am already examining creativebeartech.com, theeliquidboutique.co.uk and wowitloveithaveit.com. Unsure which one would be the most ideal choice and would appreciate any support on this. Or would it be simpler for me to scrape my own leads? Ideas?

  36. You made some good points there. I checked on the internet for more info about the issue and found most individuals will go along with your views on this web site.

  37. My brother recommended I might like this blog. He was totally right.
    This post actually made my day. You cann’t imagine simply how much time I had spent for this info!

  38. I blog quite often and I truly appreciate your content. This great article has truly peaked my interest. I am going to take a note of your site and keep checking for new details about once a week. I opted in for your Feed too.

  39. I truly love your site.. Very nice colors & theme. Did you develop this website yourself? Please reply back as I’m attempting to create my own site and would love to find out where you got this from or just what the theme is called. Thanks!

  40. I was very pleased to uncover this site. I wanted to thank you for ones time just for this wonderful read!! I definitely liked every bit of it and i also have you book marked to check out new stuff in your website.

  41. Hello there! I simply want to offer you a big thumbs up for your excellent information you have got right here on this post. I am returning to your site for more soon.

  42. I’m not sure where you are getting your info, but good topic. I needs to spend some time learning more or understanding more. Thanks for great info I was looking for this information for my mission.|

  43. That is a great tip especially to those fresh to the blogosphere. Simple but very accurate info… Thanks for sharing this one. A must read post!

  44. An impressive share! I’ve just forwarded this onto a friend who has been doing a little homework on this. And he actually ordered me lunch because I found it for him… lol. So allow me to reword this…. Thanks for the meal!! But yeah, thanks for spending some time to discuss this topic here on your blog.|

  45. Hello There. I discovered your blog the usage of msn. This is a very neatly written article. I will make sure to bookmark it and come back to read extra of your helpful information. Thanks for the post. I’ll definitely comeback.|

  46. Its like you learn my thoughts! You appear to grasp a lot approximately this, like you wrote the guide in it or something. I believe that you simply can do with some to drive the message house a bit, however instead of that, this is magnificent blog. A great read. I will definitely be back.|

  47. Greetings from Idaho! I’m bored to tears at work so I decided to browse your site on my iphone during lunch break. I enjoy the info you provide here and can’t wait to take a look when I get home. I’m shocked at how quick your blog loaded on my cell phone .. I’m not even using WIFI, just 3G .. Anyways, wonderful blog!|

  48. Its like you read my thoughts! You seem to know so much about this, such as you wrote the book in it or something. I think that you could do with a few to pressure the message home a bit, but other than that, that is fantastic blog. A fantastic read. I will certainly be back.|

  49. You ought to be a part of a contest for one of the highest quality websites on the internet. I most certainly will highly recommend this blog!

  50. Its like you read my thoughts! You seem to grasp so much approximately this, such as you wrote the e-book in it or something. I feel that you simply could do with some to drive the message home a little bit, however instead of that, this is fantastic blog. An excellent read. I’ll certainly be back.|

  51. If you are going for finest contents like me, just visit this
    web site every day for the reason that it provides feature contents, thanks

  52. Hey there! Someone in my Facebook group shared this site with us so I came to give it a look. I’m definitely loving the information. I’m bookmarking and will be tweeting this to my followers! Terrific blog and excellent design and style.|

  53. Aw, this was a very nice post. Taking the time and actual effort to create a great article… but what can I say… I hesitate a whole lot and never seem to get nearly anything done.

  54. Way cool! Some extremely valid points! I appreciate you penning this article and also the rest of the site is extremely good.

  55. Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to more added agreeable from you! However, how could we communicate?|

  56. With havin so much written content do you ever run into any issues of plagorism or copyright violation? My blog has a lot of unique content I’ve either written myself or outsourced but it appears a lot of it is popping it up all over the web without my permission. Do you know any solutions to help protect against content from being ripped off? I’d genuinely appreciate it.|

  57. An interesting discussion is definitely worth comment. I do believe that you need to write more on this subject, it may not be a taboo subject but generally folks don’t talk about these subjects. To the next! Many thanks!!

  58. You are so interesting! I don’t think I have read through anything like that before. So good to discover someone with some genuine thoughts on this issue. Really.. thank you for starting this up. This web site is something that’s needed on the web, someone with a little originality!

  59. If you are going for most excellent contents like I do, only pay a visit this site everyday for the reason that it provides quality contents, thanks|

  60. Hi! This is kind of off topic but I need some advice from an established blog. Is it difficult to set up your own blog? I’m not very techincal but I can figure things out pretty quick. I’m thinking about creating my own but I’m not sure where to start. Do you have any tips or suggestions? With thanks|

  61. I was curious if you ever considered changing the
    page layout of your website? Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so people could connect with it better.
    Youve got an awful lot of text for only having 1 or two images.
    Maybe you could space it out better?

  62. I love your blog.. very nice colors & theme. Did you make this website yourself or did you hire someone to do it for you? Plz respond as I’m looking to construct my own blog and would like to know where u got this from. thanks|

  63. It’s a pity you don’t have a donate button! I’d most certainly donate to this superb blog!
    I suppose for now i’ll settle for bookmarking and adding your RSS
    feed to my Google account. I look forward to new updates and will
    talk about this blog with my Facebook group. Talk soon!

  64. Attractive section of content. I just stumbled upon your web site and in accession capital to assert that I acquire
    in fact enjoyed account your blog posts. Any way I’ll be subscribing to your feeds
    and even I achievement you access consistently fast.

  65. Spot on with this write-up, I actually think this website needs way more consideration. I抣l in all probability be again to read much more, thanks for that info.

  66. My spouse and i felt very lucky Ervin managed to round up his survey by way of the precious recommendations he was given through your site. It is now and again perplexing to simply possibly be freely giving information and facts which usually most people may have been selling. So we acknowledge we’ve got the website owner to thank because of that. Most of the illustrations you made, the straightforward website navigation, the friendships your site give support to instill – it is everything sensational, and it’s really assisting our son and our family believe that that topic is enjoyable, and that’s incredibly important. Thanks for all the pieces!

  67. I’d like to thank you for the efforts you’ve
    put in penning this website. I really hope to view the same high-grade content from you in the future as well.
    In fact, your creative writing abilities has inspired me to get
    my very own blog now 😉

  68. I needed to write you that very little word in order to say thanks once again considering the gorgeous concepts you have featured at this time. This is certainly pretty generous of you in giving openly what a lot of people could possibly have sold as an electronic book to get some profit on their own, primarily given that you could possibly have done it in case you considered necessary. Those concepts likewise acted as the good way to be sure that many people have the identical dream just like my own to understand more and more in terms of this issue. I am sure there are a lot more fun sessions in the future for individuals that look into your website.

  69. hey there and thank you for your info – I’ve definitely picked up something new from right here. I did however expertise a few technical issues using this site, as I experienced to reload the web site a lot of times previous to I could get it to load correctly. I had been wondering if your web hosting is OK? Not that I’m complaining, but sluggish loading instances times will sometimes affect your placement in google and could damage your quality score if ads and marketing with Adwords. Anyway I am adding this RSS to my e-mail and could look out for a lot more of your respective exciting content. Ensure that you update this again very soon.|

  70. I simply wanted to develop a quick remark to appreciate you for all of the fantastic tips and tricks you are placing at this site. My considerable internet lookup has finally been recognized with awesome knowledge to write about with my company. I ‘d tell you that many of us site visitors actually are extremely fortunate to live in a notable place with many wonderful professionals with great tricks. I feel truly happy to have discovered your entire website page and look forward to many more entertaining moments reading here. Thanks a lot again for everything.

  71. My wife and i were so peaceful when Emmanuel could complete his investigation by way of the precious recommendations he came across from your web site. It is now and again perplexing to simply happen to be making a gift of secrets that many the others could have been making money from. We really discover we have the website owner to be grateful to for that. The type of illustrations you’ve made, the simple site navigation, the friendships your site aid to promote – it’s many exceptional, and it’s helping our son in addition to the family recognize that that concept is enjoyable, which is rather fundamental. Thanks for everything!

  72. Strategies For These Seeking A Fun-Packed Camping out Getaway Camping outdoors could be a wonderful way to lessen stress and in the short term get away your hectic existence. The details right here must make your vacation a fulfilling a single. Whenever you invest in a new tent for camping outdoors, input it with each other as soon as in your own home before going on your outdoor camping getaway. This helps you to have the experience needed for constructing your tent and make certain there aren’t lacking items.This will remove the frustration linked to putting together your tent. Enable everyone in the journey to possess a say about the campsite. Talk about which condition you would like to visit. There are several choices through the United states of america it really is hard to select just one. You could potentially even pick three or four possible spots and let a couple of locations to get the household vote on to make it much easier. If kids are camping with you.Be sure to do some research beforehand, go on a medical type, especially. Crisis systems are one of the most essential what you should deliver on a very important thing to have ready for any camping getaway. Take into consideration each of the alternatives that mother nature is offering, so continue to keep some anti–venom on hand. Ensure that your camp out website is complete just before it becomes darkish. If you are inside a car, park your car it safely. When you discover a camping out place, pick a dried out toned spot. Achieving this well before evening comes about allows you to inconvenience and stress. Acquire along activities to do on your own camping vacation. Once you get to the campsite, set up your tent up on the flattest, most degree soil to place your tent on. Picking a sloped or rocky area to camp out at can make your stay very unpleasant. Generally lay down a tarp as a way to end h2o from engaging in your tent. No matter what what their age is, almost everyone enjoys a calming camping journey. To be certain your next outdoor camping trip is enjoyable and effective, you have to know a few things. Hopefully, this information has presented you that knowledge.

  73. It’s perfect time to make some plans for the longer term and it’s time to be happy. I’ve read this put up and if I could I wish to counsel you some interesting issues or advice. Perhaps you could write next articles referring to this article. I want to learn more issues approximately it!|

  74. A lot of thanks for all your efforts on this website. Kim enjoys doing research and it’s obvious why. We all know all concerning the dynamic mode you deliver very important ideas on the web blog and even recommend response from the others on that concept and our own daughter is without question being taught so much. Take pleasure in the remaining portion of the new year. You’re the one performing a superb job.

  75. It is perfect time to make some plans for the future and it is time to be happy. I’ve read this post and if I could I desire to suggest you few interesting things or advice. Perhaps you can write next articles referring to this article. I desire to read even more things about it!|

  76. I was recommended this web site by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my difficulty.
    You’re wonderful! Thanks! 34pIoq5 cheap flights

  77. Hmm it appears like your site ate my first comment (it was super long) so I guess I’ll just sum it up what I wrote and say, I’m thoroughly enjoying your blog. I too am an aspiring blog blogger but I’m still new to the whole thing. Do you have any suggestions for rookie blog writers? I’d definitely appreciate it.

  78. Hello, i think that i saw you visited my website so i came to “return the prefer”.I am attempting to in finding issues to enhance my web site!I guess its ok to use some of your concepts!!

  79. I together with my pals were actually going through the best information on your web blog while before long I got a horrible feeling I never expressed respect to the website owner for those strategies. The young boys are already for this reason very interested to study all of them and already have really been having fun with those things. Many thanks for getting indeed accommodating and then for picking out this kind of wonderful areas most people are really desperate to learn about. My honest regret for not expressing gratitude to you earlier.

  80. I really like your blog.. very nice colors & theme. Did you make this website yourself or did you hire someone to do it for you? Plz reply as I’m looking to construct my own blog and would like to find out where u got this from. thank you|

  81. I appreciate, result in I discovered just what I was having a look for. You’ve ended my 4 day long hunt! God Bless you man. Have a great day. Bye|

  82. I’m commenting to make you be aware of what a remarkable discovery my friend’s daughter developed studying your web page. She picked up numerous pieces, which included how it is like to have an incredible teaching mood to have other individuals with ease master specified advanced issues. You undoubtedly surpassed our expected results. Thanks for delivering these warm and helpful, trustworthy, educational and in addition cool guidance on the topic to Evelyn.

  83. I in addition to my guys were actually going through the good solutions from your web blog and then then developed an awful suspicion I had not thanked the web blog owner for those strategies. The young men happened to be joyful to learn all of them and now have honestly been making the most of them. We appreciate you actually being considerably kind and then for considering this sort of important useful guides millions of individuals are really needing to discover. My honest regret for not saying thanks to sooner.

  84. Magnificent goods from you, man. I’ve understand your stuff previous to and you’re just extremely magnificent. I really like what you’ve acquired here, really like what you’re saying and the way in which you say it. You make it entertaining and you still take care of to keep it wise. I can’t wait to read much more from you. This is actually a tremendous site.|

  85. Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to more added agreeable from you! By the way, how could we communicate?|

  86. I every time used to read post in news papers but now as
    I am a user of web therefore from now I am using net for content, thanks
    to web.

  87. Wow that was unusual. I just wrote an really long comment but after I clicked submit my comment didn’t appear.
    Grrrr… well I’m not writing all that over again. Anyway, just wanted to say fantastic blog!

  88. Hi there! Do you know if they make any plugins to protect against hackers? I’m kinda paranoid about losing everything I’ve worked hard on. Any suggestions?|

  89. I as well as my buddies were reviewing the excellent information from your web page and so at once came up with an awful suspicion I never thanked the site owner for those techniques. Those men appeared to be consequently joyful to read all of them and now have certainly been having fun with these things. Appreciate your really being really accommodating as well as for going for varieties of exceptional ideas most people are really needing to understand about. Our own sincere regret for not expressing appreciation to sooner.

  90. Good day! This is my first comment here so I just wanted to givea quick shout out and tell you I truly enjoy reading throughyour blog posts. Can you suggest any other blogs/websites/forums that cover the same subjects?Thanks!

  91. fantastic post, very informative. I wonder why the other experts of this sector do not notice this. You should continue your writing. I’m confident, you’ve a huge readers’ base already!

  92. Everything is very open with a really clear explanation of the
    issues. It was really informative. Your site is very useful.
    Thank you for sharing!

  93. Aw, this was a very nice post. In concept I want to put in writing like this additionally ?taking time and precise effort to make a very good article?but what can I say?I procrastinate alot and on no account appear to get something done.