Home Web Front-end HTML Tutorial avalon2学习教程10事件绑定_html/css_WEB-ITnose

avalon2学习教程10事件绑定_html/css_WEB-ITnose

Jun 21, 2016 am 08:51 AM

avalon2的事件指令,比起avalon1来强大多了。

首先其内部是使用事件代理实现的,能冒泡的事件全部绑定document上。只有旧式IE的几个事件还绑定在原元素上。

其次,this直接指向vmodel,元素节点则通过e.target获取。如果要传入多个参数,需要指定事件对象,还是与原来一样使用$event

<div ms-click='@fn(111,222,$event)'>{{@ddd}}</div>
Copy after login

再次,添加了一些专门针对事件回调的过滤器

  1. 对按键进行限制的过滤器esc,tab,enter,space,del,up,left,right,down

  2. 对事件方法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>
Copy after login

<!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>
Copy after login

<!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>
Copy after login

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>
Copy after login

最后是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>
Copy after login

此外avalon还对input,animationend事件进行修复,大家也可以直接用avalon.bind, avalon.fn.bind来绑定这些事件。但建议都用ms-on绑定来处理。

Statement of this Website
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

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 ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

See all articles