javascript
JavaScript中的交互式网页/事件处理
Programming languages like C, C++ etc are all based on synchronous coding approach i.e. the execution takes place from top to bottom in a linear manner.
諸如C,C ++等編程語言均基于同步編碼方法,即執行以線性方式從上到下進行。
But JavaScript follows a different approach, it is based on asynchronous coding. Instead of traversing from top to bottom it executes through event handling.
但是JavaScript采用不同的方法,它基于異步編碼。 它不是通過上下移動而是通過事件處理執行。
Events occur and get stacked in browser processor if a handler is associated with it, it is called and executed.
如果處理程序與事件相關聯,則會發生事件并將其堆積在瀏覽器處理器中,然后調用并執行該事件 。
JS has many inbuilt objects with loads of events. In this article, we are going to use one of them i.e. window object and its resize event.
JS有許多內置對象,其中包含大量事件。 在本文中,我們將使用其中之一,即窗口對象及其調整大小事件。
Example: HTML file with internal JS within script tags
示例:在腳本標簽中帶有內部JSHTML文件
<html lang="en"><head><meta charset="utf-8"><title>Don't resize me, I'm ticklish!</title><script>function resize() {var element = document.getElementById("display");element.innerHTML = element.innerHTML + " that tickles!";}</script></head><body><p id="display">Whatever you do, don't resize this window! I'm warning you!</p><script>window.onresize = resize;</script></body> </html>HTML code is as usual. In the second script tag, I have used an inbuilt object of JS i.e. window that represents the interface of the browser in which you are running the above code. This window has many parameters, methods, events.
HTML代碼照常。 在第二個腳本標簽中,我使用了JS的內置對象,即window,它表示您在其中運行以上代碼的瀏覽器的界面。 該窗口具有許多參數,方法,事件。
Here its onresize event is being used, whenever you will change the size of your browser's window this event will be generated and get added to browser's processor stack.
在這里,將使用它的onresize事件,只要您更改瀏覽器窗口的大小,就會生成該事件并將其添加到瀏覽器的處理器堆棧中。
After that in line number 18 itself, this event has been assigned a handler by the name of resize.
在第18行本身之后,該事件已通過resize名稱分配給處理程序。
Remember:
記得:
If no handler is assigned to an event, it still gets added to processor stack but nothing happens in response to it.
如果沒有為事件分配處理程序,則該事件仍會添加到處理器堆棧中,但對此沒有任何React。
Also, note that resize is nothing but a function defined in the first script tag.
另外,請注意,調整大小不過是第一個腳本標記中定義的功能。
Caution:
警告:
You don’t need brackets in line number 18 after the function resize because you are not calling it, instead, we are just assigning it to an event as an event handler.
調整函數大小后,不需要在行號18中放置括號,因為您沒有調用它,而是將其分配給事件作為事件處理程序。
Now, in line number 7, in resize function I have accessed paragraph element of HTML file through id of the paragraph element. via DOM ( DOCUMENT OBJECT MODEL ) created by the browser.
現在,在第7行中,在調整大小功能中,我已經通過段落元素的id訪問HTML文件的段落元素。 通過瀏覽器創建的DOM(DOCUMENT OBJECT MODEL)。
Also, one should be aware that document ( in line number 8 ) is another inbuilt object in js that represents the DOM generated by the browser or in other words we can say that Javascript uses document object to get access to DOM created by the browser. Like any other inbuilt object, it too has many methods in it, one of them being getElementByID.
另外,應該知道文檔(第8行)是js中的另一個內置對象,表示由瀏覽器生成的DOM,換句話說,我們可以說Javascript使用文檔對象來訪問由瀏覽器創建的DOM。 像其他任何內置對象一樣,它也包含許多方法,其中之一就是getElementByID 。
getElementByID is seen very commonly in the source code of interactive web pages.
getElementByID在交互式網頁的源代碼中非常常見。
It takes an id as an argument and returns an OBJECT representing HTML tag with that id.
它以一個id作為參數,并返回一個表示具有該IDHTML標簽的對象。
Now this DOM object comes with many useful methods giving loads of powers to developers.
現在,此DOM對象帶有許多有用的方法,可為開發人員提供各種功能。
Its innerHTML gives access to the text inside that particular tag.
它的innerHTML可以訪問該特定標記內的文本。
Hence, appending THAT TICKLES each time the function resize is called or to be more particular the event onresize occurs.
因此,每次調用函數調整大小時,或者更具體地講,發生onresize事件時,都應添加該標題。
I hope this was a great insight into event handling in js and the most comprehensive explanation of the above-mentioned code.
我希望這對js中的事件處理有很好的了解,并且對上述代碼有最全面的解釋。
Event handling is a great asset of javascript which can be used to create heavily responsive and dynamic web pages.
事件處理是javascript的重要資產,可用于創建高度響應和動態的網頁。
I will be covering many other kinds of events available in js in my upcoming articles so STAY TUNED and keep practicing.
我將在我即將發表的文章中介紹js中可用的許多其他類型的事件,所以請繼續學習并繼續練習。
翻譯自: https://www.includehelp.com/code-snippets/interactive-webpages-event-handling-in-javascript.aspx
總結
以上是生活随笔為你收集整理的JavaScript中的交互式网页/事件处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 80版本魔王契约怎么办?等级一旦过线野猪
- 下一篇: 检查不孕多少钱啊?