Sciter中的动态元素事件处理

来源:互联网 发布:淘宝助理编辑宝贝 权重 编辑:程序博客网 时间:2024/06/09 16:12

上一篇介绍了在Sciter中静态元素订阅事件的几种方式,这次将介绍一下动态元素的事件处理以及绑定方式的区别。

<html><head><script type="text/tiscript">self.on("click", "button", function(evt) {$(body).append("<button>append</button>");});</script></head><body><button>append</button></body></html>
每点击一次按钮,就会追加一个新的<button>,而新的<button>默认会绑定到事件处理器上去,而不用手动绑定。


<html><head><script type="text/tiscript">for(var button in $$(button))button << event click { $(body).append("<button>hello</button>"); }</script></head><body><button>append</button></body></html>
而用"<< event click"的方式绑定的话,是无法对新添加的元素生效的。这是它与传统的"on"方式的一个区别,这也意味着这种方式绑定的事件处理器在内存中是独立的,不与其他元素共享。

<html><head><script type="text/tiscript">event click $(button) (evt, element) {$(body).append("<button>hello</button>");}</script></head><body><button>append</button></body></html>
最后一种方式与"on"方式绑定的结果一样,动态创建出的按钮都自动绑定到了事件处理器上。

总结:

本质上来说,"on"的方式和"event click"的方式是一样的,前者一般用于全局中,后者一般用于类中。而"element << event"这种方式会为某一个元素单独开辟内存空间,不与其他元素共享事件处理器,根据实际情况选择使用。

原创粉丝点击