avalon2学习教程10事件绑定_html/css_WEB-ITnose
avalon2的事件指令,比起avalon1来强大多了。
首先其内部是使用事件代理实现的,能冒泡的事件全部绑定document上。只有旧式IE的几个事件还绑定在原元素上。
其次,this直接指向vmodel,元素节点则通过e.target获取。如果要传入多个参数,需要指定事件对象,还是与原来一样使用$event
<div ms-click='@fn(111,222,$event)'>{{@ddd}}</div>
再次,添加了一些专门针对事件回调的过滤器
-
对按键进行限制的过滤器esc,tab,enter,space,del,up,left,right,down
-
对事件方法stopPropagation, preventDefault进行简化的过滤器stop, prevent
最后,对事件回调进行缓存,防止重复生成。
事件绑定是使用ms-on-☆绑定来实现,但avalon也提供了许多快捷方式,让用户能直接以ms-eventName调用那些常用事件,如下
animationend、 blur、 change、 input、 click、 dblclick、 focus、 keydown、 keypress、 keyup、 mousedown、 mouseenter、 mouseleave、 mousemove、 mouseout、 mouseover、 mouseup、 scan、 scroll、 submit
avalon的事件绑定支持多投事件机制(同一个元素可以绑定N个同种事件,如ms-click=fn, ms-click-1=fn2, ms-click-2=fn3)
<!DOCTYPE HTML><html> <head> <title>ms-on</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <script src="./dist/avalon.js" ></script> <script> var vm = avalon.define({ $id: "test", firstName: "司徒", array: ["aaa", "bbb", "ccc"], argsClick: function(e, a, b) { alert([].slice.call(arguments).join(" ")) }, loopClick: function(a, e) { alert(a + " " + e.type) }, status: "", callback: function(e) { vm.status = e.type }, field: "", check: function(e) { vm.field = e.target.value + " " + e.type }, submit: function() { var data = vm.$model if (window.JSON) { setTimeout(function() { alert(JSON.stringify(data)) }) } } }) </script> </head> <body> <fieldset ms-controller="test"> <legend>有关事件回调传参</legend> <div ms-mouseenter="@callback" ms-mouseleave="@callback">{{@status}}<br/> <input ms-on-input="@check"/>{{@field}} </div> <div ms-click="@argsClick($event, 100, @firstName)">点我</div> <div ms-for="el in @array" > <p ms-click="@loopClick(el, $event)">{{el}}</p> </div> <button ms-click="@submit" type="button">点我</button> </fieldset> </body></html>
<!DOCTYPE HTML><html> <head> <title>ms-on</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <script src="./dist/avalon.js" ></script> <script> var count = 0 var model = avalon.define({ $id: "multi-click", str1: "1", str2: "2", str3: "3", click0: function() { model.str1 = "xxxxxxxxx" + (count++) }, click1: function() { model.str2 = "xxxxxxxxx" + (count++) }, click2: function() { model.str3 = "xxxxxxxxx" + (count++) } }) </script> </head> <body> <fieldset> <legend>一个元素绑定多个同种事件的回调</legend> <div ms-controller="multi-click"> <div ms-click="@click0" ms-click-1="@click1" ms-click-2="@click2" >请点我</div> <div>{{@str1}}</div> <div>{{@str2}}</div> <div>{{@str3}}</div> </div> </fieldset> </body></html>
<!DOCTYPE HTML><html> <head> <title>ms-on</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <script src="./dist/avalon.js" ></script> <script> avalon.define({ $id: "xxx", fn: function() { console.log("11111111") }, fn1: function() { console.log("2222222") }, fn2: function() { console.log("3333333") } }) </script> </head> <body> <div ms-controller="xxx" ms-on-mouseenter-3="@fn" ms-on-mouseenter-2="@fn1" ms-on-mouseenter-1="@fn2" style="width:100px;height:100px;background: red;" > </div> </body></html>
avalon已经对ms-mouseenter, ms-mouseleave进行修复,可以在 这里 与 这里 了解这两个事件。到chrome30时,所有浏览器都原生支持这两个事件。
<!DOCTYPE html> <html> <head> <title>ms-mouseenter, ms-mouseleave</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <script src="./dist/avalon.js"></script> <script> avalon.define({ $id: "test", fn1: function(e) { console.log(e.type) console.log(e.target) }, fn2: function(e) { console.log(e.type) console.log(e.target) } }) </script> </head> <body ms-controller="test"> <div ms-mouseenter="@fn1" ms-mouseleave="@fn2" style="background: red;width:200px;height: 200px;padding:20px;"> <div style="background: blue;width:160px;height: 160px;margin:20px;"></div> </div> </body></html>
最后是mousewheel事件的修改,主要问题是出现firefox上,它死活也不愿意支持mousewheel,在avalon里是用DOMMouseScroll或wheel实现模拟的。我们在事件对象通过wheelDelta属性是否为正数判定它在向上滚动。
<!DOCTYPE html><html> <head> <title>ms-on-mousewheel</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <script src="./dist/avalon.js"></script> <script> var vm = avalon.define({ $id: "test", text: "", callback: function(e) { vm.text = e.wheelDelta + " " + e.type } }) </script> </head> <body ms-controller="test"> <div ms-on-mousewheel="@callback" id="aaa" style="background: red;width:200px;height: 200px;"> {{@text}} </div> </body></html>
此外avalon还对input,animationend事件进行修复,大家也可以直接用avalon.bind, avalon.fn.bind来绑定这些事件。但建议都用ms-on绑定来处理。

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











이 기사는 HTML & lt; Progress & Gt에 대해 설명합니다. 요소, 그 목적, 스타일 및 & lt; meter & gt의 차이; 요소. 주요 초점은 & lt; progress & gt; 작업 완료 및 & lt; meter & gt; Stati의 경우

이 기사는 HTML & LT; Datalist & GT에 대해 논의합니다. 자동 완성 제안을 제공하고, 사용자 경험을 향상시키고, 오류를 줄임으로써 양식을 향상시키는 요소. 문자 수 : 159

기사는 HTML5 크로스 브라우저 호환성을 보장하기위한 모범 사례에 대해 논의하고 기능 감지, 점진적 향상 및 테스트 방법에 중점을 둡니다.

이 기사는 HTML & lt; meter & gt에 대해 설명합니다. 범위 내에 스칼라 또는 분수 값을 표시하는 데 사용되는 요소 및 웹 개발의 일반적인 응용 프로그램. & lt; meter & gt; & lt; Progress & Gt; 그리고 Ex

이 기사에서는 브라우저에서 직접 사용자 입력을 검증하기 위해 필요한, Pattern, Min, Max 및 Length 한계와 같은 HTML5 양식 검증 속성을 사용하는 것에 대해 설명합니다.

이 기사는 모바일 장치의 반응 형 웹 디자인에 필수적인 Viewport Meta Tag에 대해 설명합니다. 적절한 사용이 최적의 컨텐츠 스케일링 및 사용자 상호 작용을 보장하는 방법을 설명하는 반면, 오용은 설계 및 접근성 문제로 이어질 수 있습니다.

이 기사는 & lt; iframe & gt; 외부 컨텐츠를 웹 페이지, 공통 용도, 보안 위험 및 객체 태그 및 API와 같은 대안을 포함시키는 태그의 목적.

GiteEpages 정적 웹 사이트 배포 실패 : 404 오류 문제 해결 및 해결시 Gitee ...
