<分区>
为什么 JavaScript 需要 DOM? JavaScript 究竟是如何工作的?
标签 javascript dom
<分区>
为什么 JavaScript 需要 DOM? JavaScript 究竟是如何工作的?
最佳答案
While JavaScript is the programming language which will allow you to operate on the DOM objects and to manipulate them programmatically, the DOM will provide you with methods and properties to retrieve, modify, update, and delete parts of the document you are working on. For example, you may retrieve the value of an HTML text input control as a string using the DOM. You could then use the JavaScript "+" operator to concatenate that string with another one in order to make a meaningful sentence. You would then use the DOM "alert()" method to display the string in a dialog to the user. See also the examples below.
If a Web page were a piece of imported Swedish furniture, the DOM would be the illustrations of the parts - the shelves, bolts, Allen wrenches and screwdrivers. It's possible to write instructions on how to put the parts together and use the parts in any number of languages, but you'll only use the ones written in the one you understand. The manual makes it easy to put the furniture together by using written instructions (JavaScript) to reference illustrations of objects (DOM) which represent actual objects (browser's rendering engine). (Thanks to Jonathan for the analogy!)
What's this "language-neutral" hype with the DOM? Why is the DOM language-neutral if the only language ever used to access it is JavaScript? Well, that is not quite correct. For example, Mozilla uses the DOM internally both in C++ and JavaScript for its user interface. The editor, for instance, uses the DOM extensively in order to insert, modify, and delete the HTML that you are seeing when you compose a page in the Composer module. Other known implementations of the DOM include Perl, Java, ActiveX, Python, and probably others. This is of course only possible thanks to the language-neutrality of the DOM.
关于javascript - JavaScript 和 DOM 究竟是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3299109/