search
HomeWeb Front-endJS TutorialAdvanced supplement for js dom events

Advanced supplement for js dom events

Dec 08, 2017 pm 04:51 PM
javascriptadvanced

We have shared before the method of operating dom with native js. In this article, we will follow the advanced supplement of dom events in js, hoping to help everyone.

Problems with event coverage
Clear the principle
Using event sources. The way of adding events to event types will cause coverage problems. We use a function to avoid this problem.

function addEvent(tag,fn){
    var oldClick=tag.onclick    if(typeof oldClick=="function"){
        tag.onclick=function(){
            oldClick();
            fn();
    }
    }else{
        tag.onclick=fn;
    }
}

Add events (must master)
New way to add events:
Benefits, can avoid event coverage problems
Event source.addEventListener("click ”,function(){}); Supported by ie9 and above browsers
Note: Do not add on to the type name

Event source.attachEvent(“onclick”,function(){}) Supported by lower ie versions
Note that the type name is added with on

How to remove the event
The method of adding addEventListener is to use removeEventListener to remove it
Pay attention to the writing of the second parameter

Advanced supplement for js dom events

Event source.detachEvent("onclick",fn) is used to cancel events added using attachEvent

Advanced supplement for js dom events

Original cancellation method

box.onclick=function(){}
box.onclick=null;

addEventListener compatible package.

var Event = {//添加事件。oElement:元素,sEvent:事件类型,fnHandler:绑定的函数
    addHandler: function (oElement, sEvent, fnHandler) {
        oElement.addEventListener ? oElement.addEventListener(sEvent, fnHandler, false) : oElement.attachEvent("on" + sEvent, fnHandler)    
    },//删除事件。
    removeHandler: function (oElement, sEvent, fnHandler) {
        oElement.removeEventListener ? oElement.removeEventListener(sEvent, fnHandler, false) : oElement.detachEvent("on" + sEvent, fnHandler)
    }
}
使用列子:
<input type="button" value="毫无用处的按钮"> <input type="button" value="绑定click"> <input type="button" value="解除绑定">

window.onload = function (){
    var aBtn = document.getElementsByTagName("input");    //为第一个按钮添加绑定事件
    aBtn[1].onclick = function ()
    {
        Event.addHandler(aBtn[0], "click", fnHandler);    
        aBtn[0].value = "我可以点击了"
    }    //解除第一个按钮的绑定事件
    aBtn[2].onclick = function ()
    {
        Event.removeHandler(aBtn[0], "click", fnHandler);
        aBtn[0].value = "毫无用处的按钮"    
    }    //事件处理函数
    function fnHandler ()
    {
        alert("事件绑定成功!")    
    }
}

Event bubbling and event capturing
Must master the execution order of bubbling and capturing
Event bubbling is a way of event delivery
The delivery method is : Triggered from the most specific element to the least specific element Son to parent
First triggers the event of the current element, and propagates upward after the triggering is completed. If the parent also contains this event, it is triggered, and then upwards. If not, continue looking upwards.
Events added through addEventListener, the third parameter is false to indicate event bubbling. The default is false

box1.addEventListener("click",function(){alert(1)},false)

Event capture is a way of event delivery
The execution method of event capture is from outside to inside (opposite to bubbling).
Events added through addEventListener, the third parameter is true to indicate event capture.

box1.addEventListener("click",function(){alert(1)},true)

Event object (must be mastered)
How to obtain:
1. When the event is triggered, we can receive the event object in the event handler.
This form of acquisition is not supported in lower versions of IE.

document.onmousemove=function(e){
    var e=e||window.event;//兼容ie}

2. Use window.event as the event object in lower versions of IE, which has the same function as e

Attributes of the event object (must be mastered)
Event Object.type represents the type of event. Note that the

event object.clientX and event object.clientY without on can obtain the abscissa and ordinate coordinates of the mouse for the browser's visible area when the event is triggered.

Event object.pageX and event object.pageY can obtain the abscissa and ordinate of the mouse against the left vertex of the page when the event is triggered. There is a compatibility issue. Lower versions of IE do not support it and require encapsulation functions to obtain it.
Package of page

ele.onxxx = function(event) { }
The program this points to the dom element itself
obj.addEventListener(type, fn, false);
The program this points to the dom element itself obj.attachEvent('on' + type, fn); Program this points to window

function getPage(e){var e=e||window.event;var src=scroll()//这个函数是在dom高级里面讲到对scrollLeft和scrollTop的封装;
    return {
     PageX:scroll.scrollleft+e.clientX,
      PageY:scroll.scrolltop+e.clienttop,
    }
}
封装兼容性的addEvent(elem, type, handle);方法function addEvent(elem, type, handle) {    if(elem.addEventListener) {
        elem.addEventListener(type, handle, false);
    }else if(elem.attachEvent) {
         elem[&#39;temp&#39;] = function() {
            handle.call(elem);
        }
        elem.attachEvent(&#39;on&#39; + type, elem[&#39;temp&#39;]);
    }else{
        elem[&#39;on&#39; + type] = handle;
    }
}


What are the advantages and disadvantages of event bubbling and event capturing.


(1) Bubble-type events: Events are triggered in order from the most specific event target to the least specific event target (document object).

IE 5.5: p -> body -> document

IE 6.0: p -> body -> html -> document Mozilla 1.0: p -> body -> html -> document -> window (2) Capturing event (event capturing): The event is triggered starting from the least precise object (document object), and then to Most accurate (events can also be captured at the window level, but this must be specifically specified by the developer).

(3) DOM event flow: supports two event models at the same time: capturing events and bubbling events, but the capturing events occur first. Both event streams will touch all objects in the DOM, starting from the document object and ending with the document object.

The most unique property of the DOM event model is that text nodes also trigger events (not in IE).

Typical example of event bubbling

The idea of ​​bubbling is to listen for events on ancestor nodes and combine event.target/event.srcElement to achieve the final effect. The effect is equivalent to the following code:

封装的兼容方法function removeEvent(elem, type, handle) {
    if(elem.removeEventListener) {
        elem.removeEventListener(type, handle, false);
    }else if(elem.detachEvent) {
        elem.detachEvent(&#39;on&#39; + type, handle);
    }else{
        elem[&#39;on&#39; + type] = null;
    }
}


Stop event bubbling
event.stopPropagation(); // 阻止事件冒泡return false;既能阻止默认事件又能 阻止事件冒泡

当用户名为空时,单击“提交”按钮,会出现提示,并且表单不能提交。只有在用户名里输入内容后,才能提交表单。可见,preventDefault()方法能阻止表单的提交行为。
如果想同时对事件对象停止冒泡和默认行为,可以在事件处理函数中返同false。这是对在事件对象上同时调用stopPrapagation()方法和preventDefault()方法的一种简写方式。
在表单的例子中,可以把 event.preventDefault(); 改写为: return false;
也可以把事件冒泡例子中的 event.stopPropaqation(); 改写为: return false;

事件覆盖的问题
清楚原理
使用事件源.事件类型的添加事件方式会产生覆盖问题。我们通过一个函数去避免这个问题。

function addEvent(tag,fn){
    var oldClick=tag.onclick    if(typeof oldClick=="function"){
        tag.onclick=function(){
            oldClick();
            fn();
    }
    }else{
        tag.onclick=fn;
    }
}

添加事件(必须掌握)
自带的添加事件新方式:
好处,可以避免事件覆盖问题
事件源.addEventListener(“click”,function(){});   ie9以上浏览器支持
注意:类型名不加 on

事件源.attachEvent(“onclick”,function(){})  ie低版本支持
注意,类型名加on

移除事件的方式
addEventListener 的添加方式使用removeEventListener进行移除
注意第二个参数的写法

Advanced supplement for js dom events

事件源.detachEvent(“onclick”,fn)用于取消使用attachEvent添加的事件

Advanced supplement for js dom events

原始的取消方式

box.onclick=function(){}
box.onclick=null;

addEventListener兼容封装。

var Event = {//添加事件。oElement:元素,sEvent:事件类型,fnHandler:绑定的函数
    addHandler: function (oElement, sEvent, fnHandler) {
        oElement.addEventListener ? oElement.addEventListener(sEvent, fnHandler, false) : oElement.attachEvent("on" + sEvent, fnHandler)    
    },//删除事件。
    removeHandler: function (oElement, sEvent, fnHandler) {
        oElement.removeEventListener ? oElement.removeEventListener(sEvent, fnHandler, false) : oElement.detachEvent("on" + sEvent, fnHandler)
    }
}
使用列子:
<input type="button" value="毫无用处的按钮"> <input type="button" value="绑定click"> <input type="button" value="解除绑定">

window.onload = function (){
    var aBtn = document.getElementsByTagName("input");    //为第一个按钮添加绑定事件
    aBtn[1].onclick = function ()
    {
        Event.addHandler(aBtn[0], "click", fnHandler);    
        aBtn[0].value = "我可以点击了"
    }    //解除第一个按钮的绑定事件
    aBtn[2].onclick = function ()
    {
        Event.removeHandler(aBtn[0], "click", fnHandler);
        aBtn[0].value = "毫无用处的按钮"    
    }    //事件处理函数
    function fnHandler ()
    {
        alert("事件绑定成功!")    
    }
}

事件冒泡和事件捕获
必须掌握冒泡和捕获的执行顺序
事件冒泡是事件传递的一种方式
传递方式为:由最特定的元素触发到最不特定的元素  子向父
首先触发当前元素的事件,触发完毕向上传播,如果父级也含有这个事件,触发,再向上,如果没有直接继续向上寻找。
通过addEventListener添加的事件,第三个参数为false表示事件冒泡。默认为false

box1.addEventListener("click",function(){alert(1)},false)

事件捕获是事件传递的一种方式
事件捕获的执行方式,是由外向内(跟冒泡相反)。
通过addEventListener添加的事件,第三个参数为true表示事件捕获。

box1.addEventListener("click",function(){alert(1)},true)

事件对象(必须掌握)
获取方式:
1、当事件触发时,我们可以通过在事件处理程序中接收事件对象。
这种获取形式在ie低版本不支持。

document.onmousemove=function(e){
    var e=e||window.event;//兼容ie}

2、在ie低版本中使用window.event作为事件对象,作用和e相同

事件对象的属性(必须掌握)
事件对象.type 表示事件的类型,注意是不加on的

事件对象.clientX和事件对象.clientY可以获取事件触发时鼠标针对浏览器可视区域的横坐标和纵坐标。

事件对象.pageX和事件对象.pageY可以获取事件触发时鼠标针对页面左顶点的横坐标和纵坐标。 有兼容性问题,ie低版本不支持,需要封装函数获取。
pageX和pageY的封装

function getPage(e){var e=e||window.event;var src=scroll()//这个函数是在dom高级里面讲到对scrollLeft和scrollTop的封装;
    return {
     PageX:scroll.scrollleft+e.clientX,
      PageY:scroll.scrolltop+e.clienttop,
    }
}

onmousemove 鼠标移动时触发
onmousedown 鼠标点下时触发
onmouseup    鼠标抬起时触发
事件处理程序的运行环境
ele.onxxx = function(event) { }  
程序this指向是dom元素本身
obj.addEventListener(type, fn, false);  
程序this指向是dom元素本身
obj.attachEvent(‘on’ + type, fn);  
程序this指向window

封装兼容性的addEvent(elem, type, handle);方法function addEvent(elem, type, handle) {    if(elem.addEventListener) {
        elem.addEventListener(type, handle, false);
    }else if(elem.attachEvent) {
         elem[&#39;temp&#39;] = function() {
            handle.call(elem);
        }
        elem.attachEvent(&#39;on&#39; + type, elem[&#39;temp&#39;]);
    }else{
        elem[&#39;on&#39; + type] = handle;
    }
}
封装的兼容方法function removeEvent(elem, type, handle) {
    if(elem.removeEventListener) {
        elem.removeEventListener(type, handle, false);
    }else if(elem.detachEvent) {
        elem.detachEvent(&#39;on&#39; + type, handle);
    }else{
        elem[&#39;on&#39; + type] = null;
    }
}

事件冒泡和事件捕获有什么好处和弊端。

(1)冒泡型事件:事件按照从最特定的事件目标到最不特定的事件目标(document对象)的顺序触发。
 IE 5.5: p -> body -> document
 IE 6.0: p -> body -> html -> document
 Mozilla 1.0: p -> body -> html -> document -> window
(2)捕获型事件(event capturing):事件从最不精确的对象(document 对象)开始触发,然后到最精确(也可以在窗口级别捕获事件,不过必须由开发人员特别指定)。
(3)DOM事件流:同时支持两种事件模型:捕获型事件和冒泡型事件,但是,捕获型事件先发生。两种事件流会触及DOM中的所有对象,从document对象开始,也在document对象结束。
 DOM事件模型最独特的性质是,文本节点也触发事件(在IE中不会)。
事件冒泡典型的例子
冒泡的思路是在祖先节点上监听事件,结合event.target/event.srcElement来实现最终效果,其效果等同于如下代码:

<p class="J_rate" onmouseover="..." onmouseout="..." onclick="...">
  <img  src="/static/imghwm/default1.png"  data-src="star.gif"  class="lazy"   title="很烂" / alt="Advanced supplement for js dom events" >
  <img  src="/static/imghwm/default1.png"  data-src="star.gif"  class="lazy"   title="一般" / alt="Advanced supplement for js dom events" >
  <img  src="/static/imghwm/default1.png"  data-src="star.gif"  class="lazy"   title="还好" / alt="Advanced supplement for js dom events" >
  <img  src="/static/imghwm/default1.png"  data-src="star.gif"  class="lazy"   title="较好" / alt="Advanced supplement for js dom events" >
  <img  src="/static/imghwm/default1.png"  data-src="star.gif"  class="lazy"   title="很好" / alt="Advanced supplement for js dom events" >
 </p>
 // 五心好评的例子,不用给img添加,直接给父元素加

停止事件冒泡

event.stopPropagation(); // 阻止事件冒泡return false;既能阻止默认事件又能 阻止事件冒泡

当用户名为空时,单击“提交”按钮,会出现提示,并且表单不能提交。只有在用户名里输入内容后,才能提交表单。可见,preventDefault()方法能阻止表单的提交行为。
如果想同时对事件对象停止冒泡和默认行为,可以在事件处理函数中返同false。这是对在事件对象上同时调用stopPrapagation()方法和preventDefault()方法的一种简写方式。
在表单的例子中,可以把 event.preventDefault(); 改写为: return false;
也可以把事件冒泡例子中的 event.stopPropaqation(); 改写为: return false;

相关推荐:

jquery之dom学习

原生js 操作dom

jQuery操作DOM的方法

The above is the detailed content of Advanced supplement for js dom events. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)Apr 11, 2025 am 08:22 AM

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript: Exploring the Versatility of a Web LanguageJavaScript: Exploring the Versatility of a Web LanguageApr 11, 2025 am 12:01 AM

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The Evolution of JavaScript: Current Trends and Future ProspectsThe Evolution of JavaScript: Current Trends and Future ProspectsApr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)