简体   繁体   English

我该如何连结 <div> javascript对象(没有框架)? (Actionscript概念到JS world)

[英]How do I link <div> to javascript object (without framework)? (Actionscript concepts to JS world)

I'm a relative beginner to Javascript. 我是Java语言的相对入门者。 I come from a background in Flash OOP programming (Actionscript 3), where there was an inherent and explicit relationship between on-screen graphical objects and a Class . 我来自Flash OOP编程的背景知识(动作脚本3),其中在屏幕上的图形对象和Class之间存在固有的显式关系。 I'm trying to figure out if it's possible to translate this direct and explicit relationship to the Javascript/HTML world. 我试图找出是否有可能将这种直接和显式的关系转换为Javascript / HTML世界。

For instance, if I have an online game and had various graphical areas -- a play area, an input area, a scoreboard. 例如,如果我有一个在线游戏,并且具有各种图形区域-游戏区域,输入区域,记分板。 I'd like interactions with each of those should be handled by a separate object or prototype (or class or module). 我希望与每个交互都应由单独的对象或原型(或类或模块)处理。

My first thought is to do it through event delegation , adding an event listener to the parent graphical object that covers the area I'm interested in and linking that to a "master method" in the code Object (or Class ) that "represents" that graphical object. 我的第一个想法是通过事件委托来做到这一点,将事件侦听器添加到覆盖我感兴趣的区域的父图形对象,并将其链接到“表示”代码的Object (或Class )中的“主方法”。该图形对象。 From there I could figure out what part of the object was clicked on and reference methods inside the object. 从那里,我可以弄清楚单击对象的哪个部分以及对象内部的引用方法。 This seems an awkward route to go down. 这似乎是一条尴尬的路线。

Or I could simply add event listeners to the individual components in the graphical object and them link to event handlers inside the code object which represents that graphical object. 或者,我可以简单地将事件侦听器添加到图形对象中的各个组件,并将它们链接到表示该图形对象的代码对象内的事件处理程序。 I think this is probably the right answer. 我认为这可能是正确的答案。

I just wanted to ask whether there any other way to link entire graphical DOM objects and entire code Objects (or classes, prototypes, etc), one that more closely mimics that direct connection that Flash offered between one graphical object and one code structure? 我只是想问问是否还有其他方法可以链接整个图形DOM对象和整个代码Objects (或类,原型等),而这种方法更紧密地模仿了Flash在一个图形对象和一个代码结构之间提供的直接连接吗?

In the JavaScript world where there is a web UI to work with, the Document Object Model (DOM) API is your gateway toward connecting JavaScript objects to the HTML elements that make up a page. 在具有Web UI的JavaScript世界中, 文档对象模型(DOM)API是将JavaScript对象连接到组成页面的HTML元素的网关。

While there are many aspects to what the DOM is and can do, it starts with getting a JavaScript reference to a single HTML element (a "node" reference) or to a group of HTML elements (a "node list" reference). 尽管DOM有很多方面和可以做什么,但它首先要获取对单个HTML元素(“节点”引用)或一组HTML元素(“节点列表”引用)的JavaScript引用。 We don't make distinctions about whether those elements are "graphical" or not. 我们不区分这些元素是否是“图形”元素。 We think of each of these elements generically, as "nodes". 我们将这些元素统称为“节点”。 They are part of the HTML document and therefore they are part of a "tree" of elements that we can access. 它们是HTML文档的一部分,因此它们是我们可以访问的元素“树”的一部分。

Getting your references can be done in a variety of ways (the id of the element, the CSS class(es) used by the element(s), the hierarchical position of the element in the larger document structure, etc.). 获取引用可以通过多种方式完成(元素的id ,元素使用的CSS类,元素在较大文档结构中的分层位置等)。

Here are some examples: 这里有些例子:

 // Get a DOM node reference via its id var theDiv = document.getElementById("firstDiv"); console.log(theDiv.textContent); // Get a DOM node reference based on its position in the document var theSecondDiv = document.querySelector("#firstDiv + div"); // Sibling following the one with the id console.log(theSecondDiv.textContent); // Get a node list of all the elements that have the "special" class var theSpecials = document.querySelectorAll(".special"); console.log(theSpecials[0].textContent, theSpecials[1].textContent, theSpecials.length); // Get a node reference based on having a certain attribute value var theButton = document.querySelector("input[type='button']"); theButton.value = "I have a new value!"; 
 <div id="firstDiv">I'm a div</div> <div>I'm the second div</div> <p class="special">I'm a paragraph with a class</p> <h2 class="special">I'm an h2 with a class</h2> <input type="button" value="Button"> <input type="text"> 

Once you have your DOM reference, the next step is to interact with the object and this could mean getting/setting property values, calling methods or configuring event callback functions. 获得DOM参考后,下一步就是与对象进行交互,这可能意味着获取/设置属性值,调用方法或配置事件回调函数。

For the most part, HTML attributes will map to JavaScript object properties. 在大多数情况下,HTML属性将映射到JavaScript对象属性。 In the last example the value attribute of the <input type="button"> mapped to the value property of the JavaScript DOM object reference ( theButton.value = ... ). 在最后一个例子的value属性 <input type="button">映射到value JavaScript的DOM对象引用( 财产 theButton.value = ... )。 In addition to the attributes of the original HTML element being translated into properties, the DOM API endows DOM object references with additional properties. 除了将原始HTML元素的属性转换为属性外,DOM API还为DOM对象引用赋予了其他属性。 For example, all nodes have a nodeName , nodeType and nodeValue property and nodes that represent elements that have an opening and closing tag to them will have a .textContent and an .innerHTML property to get/set the content as text or HTML. 例如,所有节点都具有nodeNamenodeTypenodeValue属性,表示具有开始和结束标记的元素的节点将具有.textContent.innerHTML属性,以将内容获取/设置为文本或HTML。 Other, more unique types of DOM references will have more specific properties. 其他更独特的DOM引用类型将具有更多特定的属性。

The DOM API also specifies various methods for DOM objects. DOM API还为DOM对象指定了各种方法。 Form elements, for example, will have a .focus() method for setting the UI's focus on that element, as if the user had tabbed to the element or clicked into it. 例如,表单元素将具有.focus()方法,用于设置UI在该元素上的焦点,就好像用户已将其切换到该元素或单击该元素一样。

Now, to your question... 现在,您的问题...

Just about all DOM elements support various events and you can configure a single element to respond to one or more events or you can take advantage of "event bubbling" and set an event handler on an ancestor element and wait for events triggered in descendants to bubble up to the ancestor. 几乎所有DOM元素都支持各种事件,您可以配置单个元素以响应一个或多个事件,也可以利用“事件冒泡”并在祖先元素上设置事件处理程序,并等待后代中触发的事件冒泡取决于祖先。 This is the "event delegation" that you referred to in your question. 这是您在问题中提到的“事件委托”

Here are examples of both: 以下是两个示例:

 // Set an event handler for the <p> element document.querySelector("p").addEventListener("click", function(){ console.log("You clicked the p"); }); // Event delegation is beneficial in two circumstances. // 1. When a single function should be used to handle the same event for many different elements // and you don't want to have to store that callback function with all the different elements // FYI: all event handlers are automatically passed a reference to the event object that they are handling document.getElementById("parent").addEventListener("click", function(evt){ console.log("The event was handled at the parent, but it was triggered at: " + evt.target); }); // 2. When elements will be dynamically added to the document and you can't statically set up event // handlers for elements that don't exist yet. let span = document.createElement("span"); span.textContent = "I was dynamically created, but if you click me, I'll immediately work!"; document.getElementById("parent").appendChild(span); 
 <div id="parent"> <p>Child One (p)<p> <div>Child Two (div)</div> </div> 

As you can see, event delegation has its place, but unless you have one of the use cases for event delegation, binding individual objects to event handlers is generally the norm. 如您所见,事件委托有其地位,但是除非您有事件委托的用例之一,否则通常将单个对象绑定到事件处理程序。

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

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