简体   繁体   English

如何掌握文档对象模型(DOM)?

[英]How to master Document Object Model (DOM)?

Been using Javascript libraries like Prototype and jQuery for development, for a while now. 一段时间以来一直使用Prototype和jQuery等Javascript库进行开发。 While its a boon using those libraries, the one downside is that using such libraries probably doesn't help in understanding what happens under the hood. 尽管使用这些库是一个好处,但一个缺点是使用这样的库可能无助于理解底层发生的事情。

  1. So how does one master DOM? 那么如何掌握DOM呢?
  2. Will understanding DOM be of more help when using Javascript libraries? 在使用Javascript库时,理解DOM会有更多帮助吗?
  3. When not using libraries, what benefits does one have by understanding DOM? 不使用库时,通过理解DOM会有什么好处?

Many Thanks in advance 提前谢谢了

  1. So how does one master DOM? 那么如何掌握DOM呢?
    This is almost as general as saying "How does one master programming?", but I'll give it a shot. 这几乎和“一个主编程怎么样?”一样通用,但我会试一试。

    Practice is key. 实践是关键。 You can't just read a few books and become good with it. 你不能只是阅读一些书并且变得很好。 You need practice, and experience to know what exactly is going on when you do different things, and how different browsers interpret what you're doing. 你需要练习和经验才能知道当你做不同的事情时到底发生了什么,以及不同的浏览器如何解释你正在做的事情。

    Of course, there are things you can do, one is to start taking tutorials at HTML Dog (check out JavaScript & HTML DOM in left menu). 当然,有些事情你可以做,一个是开始在HTML Dog上学习教程(在左侧菜单中查看JavaScript和HTML DOM)。 Also, check out caniuse.com so you can see what is supported in the DOM. 另外,请查看caniuse.com,以便了解DOM支持的内容。 Blogs are a great help, too. 博客也是一个很好的帮助。 John Resig's blog (creator of jQuery) is a good resource for some more advanced DOM techniques. John Resig的博客 (jQuery的创建者)是一些更高级的DOM技术的良好资源。 Taking a look into how JavaScript libraries' code to see how they do things. 看看JavaScript库的代码如何看待它们是如何做的。 This will give you some ideas as to where to start. 这将为您提供从哪里开始的一些想法。 Of course, you need to have some knowledge & understanding of the DOM for this to be useful, first. 当然,首先需要对DOM有一些了解和理解才能使其有用。 The DOM is a mess , so it can take lots of time and patience to really master it. DOM是一团糟 ,因此需要花费大量的时间和耐心才能真正掌握它。

    You'll need to master the following, in a cross browser way, which is why it's much better to use an already written framework. 您需要以跨浏览器的方式掌握以下内容,这就是使用已编写的框架更好的原因。

    • DOM manipulation DOM操作
    • Event handling 事件处理
    • AJAX requests AJAX请求
      .
  2. Will understanding DOM be of more help when using Javascript libraries? 在使用Javascript库时,理解DOM会有更多帮助吗?
    Yes. 是。 JavaScript libraries are great, but you can do some very unfavorable things in JavaScript frameworks if you don't know what they framework is (essentially) doing behind the scenes. JavaScript库很棒,但如果你不知道他们的框架(基本上)在幕后做什么,你可以在JavaScript框架中做一些非常不利的事情。 For example, jQuery selectors are designed to traverse the DOM in a specific way. 例如,jQuery选择器旨在以特定方式遍历DOM。 If you don't understand how jQuery traversal works, it can have significant performance implications depending on your circumstances. 如果您不了解jQuery遍历的工作原理,则根据您的具体情况,它可能会产生重大的性能影响。

  3. When not using libraries, what benefits does one have by understanding DOM? 不使用库时,通过理解DOM会有什么好处?
    Well, when not using a JavaScript library, you have to have a pretty solid understanding of the DOM to do anything in JavaScript. 好吧,当不使用JavaScript库时,你必须非常了解DOM才能在JavaScript中做任何事情。 It can be hard to develop something that is maintainable that works across all browsers if you aren't comfortable with everything in the DOM. 如果您对DOM中的所有内容都不满意,那么很难开发出可以在所有浏览器中运行的可维护的东西。

Of course, you should almost never do development without a JavaScript framework . 当然, 如果没有JavaScript框架您几乎不应该进行开发 Pick one that meets your needs and master it. 选择一个满足您需求并掌握它的人。 It's much better than trying to re-invent the wheel (unless you plan on learning more about wheels) . 它比试图重新发明轮子要好得多(除非你打算更多地学习轮子)

  1. By writing JS without a library or by studying the code of a library 通过编写没有库的JS或通过研究库的代码
  2. Yes. 是。 For example, you will understand why doing certain actions using a library can be slow and how to do them better 例如,您将理解为什么使用库执行某些操作可能会很慢以及如何更好地执行它们
  3. If you're not using a library you'd actually have to understand the DOM to be able to do anything ;) 如果您没有使用库,您实际上必须了解DOM才能执行任何操作;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM