简体   繁体   English

帮我了解DOM中的对象

[英]Help me understand objects in DOM

I have a question about DOM objects. 我对DOM对象有疑问。

What I have learned is that everything in a document are objects? 我了解到文档中的所有内容都是对象?

<form name="myform" action="script.js">
    <input type="text">type something</input>
</form>

If we look at the example, is form an object with properties name and action ? 如果我们看一下例子,是form具有属性的对象nameaction

But are name and action themselves also objects? 但是, nameaction本身也是对象吗? i don't quite understand the difference between objects and properties. 我不太了解对象和属性之间的区别。

And what is the difference between object element and node? 对象元素和节点之间有什么区别?

I know basics of html, css, javascript and dom. 我知道html,css,javascript和dom的基础知识。 but can someone describe the big picture for me here how these parts are communicating with each other. 但是有人可以在这里为我描述大局,这些部分是如何相互交流的。

because there are so many elements, properties and methods. 因为有太多的元素,属性和方法。 so im all lost 所以我全丢了

Seems to me your trouble is not understanding the concept behind object-oriented programming. 在我看来,您的麻烦是不了解面向对象编程背后的概念。 JavaScript is an object-oriented language. JavaScript是一种面向对象的语言。 You should probably take a quick tour of the concepts . 您可能应该快速浏览一下这些概念

Briefly, though, objects are a way to encapsulate both data (the "properties" you are referring to) and functionality (the "methods" or "functions" you can ask an object to perform). 简而言之,对象是一种封装数据(您所指的“属性”)和功能(您可以要求对象执行的“方法”或“功能”)的方法。

In the case of the JavaScript DOM, there is a tree of objects, where each object contains properties that are, themselves, objects. 对于JavaScript DOM,有一个对象树,其中每个对象都包含本身就是对象的属性。 So you can have a Form object with methods like onSubmit() and properties like "elements", which is an array of form fields. 因此,您可以拥有一个Form对象,该对象具有诸如onSubmit()之类的方法和诸如“元素”之类的属性,该属性是一组表单字段。 Each element in the array is another object, like perhaps a TextField object or a Checkbox. 数组中的每个元素都是另一个对象,例如TextField对象或Checkbox。

So now you know about objects. 所以现在您了解对象了。 One thing about objects is that they can inherit properties from a parent class of object. 关于对象的一件事是它们可以从对象的父类继承属性。 For example, in JavaScript, there is an Element class and each of the form field objects is an instance of a "subclass" of the Element class. 例如,在JavaScript中,有一个Element类,并且每个表单字段对象都是Element类的“子类”的一个实例。 So, since Element defines the "name" property, and TextField and Checkbox and all their friends inherit from Element, they all automatically have this "name" property available too. 因此,由于Element定义了“ name”属性,并且TextField和Checkbox及其所有朋友都从Element继承,因此他们也都自动拥有了该“ name”属性。

The term "node" refers to a particular location in a tree or graph structure. 术语“节点”是指树或图结构中的特定位置。 In this case, the DOM (Document Object Model) defines the types of objects that are allowed to be nodes in the tree that represents the webpage. 在这种情况下,DOM(文档对象模型)定义了允许作为代表网页的树中节点的对象的类型。 For each webpage you visit, the browser constructs a "DOM tree", which is a big tree of objects representing each of the elements in the webpage. 对于您访问的每个网页,浏览器都会构造一个“ DOM树”,该树是代表该网页中每个元素的大对象树。

Notice that HTML is naturally in a tree-like structure: the html tag contains head and body, the head tag contains title, meta, and script tags, the meta tag contains attributes like name and content. 请注意,HTML自然是树状结构:html标签包含head和body,head标签包含title,meta和script标签,meta标签包含名称和内容之类的属性。 All this is arranged by the browser into a tree of objects, and that is what you are manipulating when you use JavaScript to do DOM programming. 所有这些都由浏览器安排成一个对象树, 就是使用JavaScript进行DOM编程时要处理的内容。

So to recap: objects are the fundamental representation of data and functionality in JavaScript. 回顾一下:对象是JavaScript中数据和功能的基本表示。 Elements are particular classes of object that are subclasses of the "Element" class, and which represent some kind of form field. 元素是对象的特定类,是“元素”类的子类,并且代表某种形式的字段。 These can be found in the form.elements array, which is a property of the form object. 这些可以在form.elements数组中找到,该数组是form对象的属性。 Finally, nodes are points in the tree of tags, text, script, and other objects that make up a webpage. 最后,节点是构成网页的标签,文本,脚本和其他对象树中的点。

Hope it helps! 希望能帮助到你!

I've taught people about the DOM before and found that seeing the DOM in action. 我之前曾教过人们有关DOM的知识,发现看到DOM的实际效果。

Go get firebug, if you don't have it. 如果您没有萤火虫,去吧。 http://getfirebug.com/ http://getfirebug.com/

After you of course restart Firefox... 当然,重新启动Firefox之后...

Open up any web page and right-click on something, there will be an option called "Inspect Element". 打开任何网页并右键单击某些内容,将有一个名为“检查元素”的选项。 This will display the rendered code of the page. 这将显示页面的渲染代码。 When I say rendered I'm referring to the page after it has been modified by any javascript, different from the standard source code which is just the straight HTML sent from the server. 当我说渲染时,我指的是在页面被任何Javascript修改后,它不同于标准源代码,后者只是从服务器发送的纯HTML。

The element which you are inspecting will be highlighted. 您正在检查的元素将突出显示。 Now, right click on that highlighted element and select "Inspect in DOM Tab". 现在,右键单击该突出显示的元素,然后选择“在DOM选项卡中检查”。

Once you're in the DOM tab, this shows you all of the properties of the element you inspected. 进入DOM选项卡后,这将向您显示所检查元素的所有属性。 In here you may see attributes of the element like type="input", methods of the element like focus(), maybe custom prototypes, Firefox proprietary attributes, and much much more. 在这里,您可能会看到元素的属性(例如type =“ input”),元素的方法(例如focus()),自定义原型,Firefox专有属性等等。

Here are some methods, functions, and attributes on your need to know list, look these up in the Mozilla Developer Center. 以下是您需要了解的一些方法,函数和属性,请在Mozilla开发人员中心中查找。 Spend some time writing scripts which move you around the DOM and allow you to alter it using these, don't use innerHTML to do anything. 花一些时间编写脚本,这些脚本可以使您在DOM周围移动,并允许您使用这些脚本进行更改,而不要使用innerHTML来做任何事情。

  • createElement 的createElement
  • createTextNode 一个createTextNode
  • appendChild 使用appendChild
  • insertBefore 的insertBefore
  • removeChild removeChild之
  • cloneNode cloneNode
  • setAttribute 的setAttribute
  • removeAttribute 的removeAttribute
  • getElementById 的getElementById
  • getElementsByTagName 的getElementsByTagName
  • childNodes 的childNodes
  • firstChild 第一个孩子
  • lastChild 最后一个孩子
  • nextSibling nextSibling
  • hasChildNodes hasChildNodes
  • tagName 标签名称
  • attachEvent (IE) / addEventListener (all others) attachEvent(IE)/ addEventListener(所有其他)
  • detachEvent (IE) / removeEventListener (all others) detachEvent(IE)/ removeEventListener(所有其他)

HTML attributes are exposed as properties on the element object. HTML属性作为元素对象的属性公开。

Element = object, property = attribute. 元素=对象,属性=属性。 A method is just an invocable property, eg onclick. 方法只是一个无法调用的属性,例如onclick。

<form
onsubmit="alert('hi');"
id="contact-form"
>

onsubmit is a method, id is a property, form is a node with a node type of 1 which means it's an HTMLElement and inherits generic HTML properties. onsubmit是一个方法, id是一个属性, form是一个节点类型为1的节点,这意味着它是HTMLElement并继承了通用HTML属性。

in your example, the name and action are attributes of the form object. 在您的示例中,名称和操作是表单对象的属性。 if you had something like this in a JSP page 如果您在JSP页面中有这样的内容

<form name="frm" action="somepage.jsp" onSubmit=" return validateForm();">
  <input type="text" name="txtField" id="txtField"></input>
  <input type="submit" value="submit"></input>
</form>

and a js function defined like so that checks to see if any text was entered into a text box. 和一个定义为的js函数,以便检查是否在文本框中输入了任何文本。

<script>
  function validateForm(){
    if(document.frm.txtField.value="")
       return false;
    else
       return true;
  }
 </script>

you can use the document object to access the form object in the dom and from that access the input element held by the form. 您可以使用文档对象访问dom中的表单对象,并从该对象访问表单所保存的输入元素。

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

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