1. Two ways to bind a method to an event.
1. Using tag binding:
<html> <head> <script type="text/javascript"> function display(){ var str = "hello world"; alert(str); } </script> </head> <body onmousedown="display();"> </body> </html>
2. Using callback function assignment:
<html> <head> <script type="text/javascript"> function display(){ var str = "hello world"; alert(str); } document.onmousedown = display; </script> </head> <body> </body> </html>
Comments;
1. The first approach is more flexiable cause we can assign callbacks dynamically.
2. The second approach is more intuitive compared with the first.
Example:
1. Works fine.
<html> <head> <script type="text/javascript"> function display(){ var str = "hello world"; alert(str); } </script> </head> <body> <button onclick="display();" value="CLICK ME" /> </body> </html>
2. Cannot find the object cause the html parse procedure is from front to end.
<html> <head> <script type="text/javascript"> // function display(){ // var str = "hello world"; // alert(str); // } var btn = document.getElementById("button"); alert(btn); </script> </head> <body> <input type="button" id="button" value="click me" /> </body> </html>
// Would alert NULL. // Cause when we execute the script, the button hasn't been initialized yet. // If we move the scriptlet to the end of <body> tag, it works fine.
3. A better solution for loading null.
<html> <head> <script type="text/javascript"> function display(){ var str = "hello world"; alert(str); } function init(){ var btn = document.getElementById("button"); btn.onclick = display; //Callbacks binding. } </script> </head> <body onload="init();"> <input type="button" id="button" value="click me" /> </body> </html>
Below is using a anonymous function.
<html> <head> <script type="text/javascript"> function init(){ var btn = document.getElementById("button"); btn.onclick = function(){ var str = "hello world"; alert(str); }; } </script> </head> <body onload="init();"> <input type="button" id="button" value="click me" /> </body> </html>
P.S.
1. For event handler, an event object will be passed into it.
For IE/Firefox, code below will both works fine. Cause there will be an inner event object named 'event' passed into the callbacks.
<html> <head> <script type="text/javascript"> function init(){ var btn = document.getElementById("button"); btn.onclick = display; } function display(){ var str = "hello world"; alert(event.type + " " + str) } </script> </head> <body onload="init();"> <input type="button" id="button" value="click me" /> </body> </html>
2. For Firefox, code below works fine but it is not the case when using IE
<html> <head> <script type="text/javascript"> function init(){ var btn = document.getElementById("button"); btn.onclick = display; } function display(event){ var str = "hello world"; alert(event.type + " " + str) } </script> </head> <body onload="init();"> <input type="button" id="button" value="click me" /> </body> </html>
<html> <head> <script type="text/javascript"> function init(){ var btn = document.getElementById("button"); btn.onclick = display; } function display(event1){ var str = "hello world"; alert(event1.type + " " + str) } </script> </head> <body onload="init();"> <input type="button" id="button" value="click me" /> </body> </html>
// click hello world
So we have to take the different implementation of JS into account when developing multi-browser app.
In IE, "event" is a pre-defined object like that of "document" that is instantiated by SYSTEM/Browser automatically.
In FIREFOX, an event object will be instantionlized and passed into the callback function.
3. BTW, what if we want to pass params into callbacks?
How can we handle the existance of "event" and self-defined param?
<html> <head> <script type="text/javascript"> function init(){ var btn = document.getElementById("button"); btn.onclick = display; } function display(){ var str = "hello world"; alert(this.value + " " + str); } </script> </head> <body onload="init();"> <input type="button" id="button" value="click me" /> </body> </html>
Comments: Here, "this" represents the object that called this function that is the button.
Result would be : "click me hello world".
So we don't have to pass param into the callbacks, cause it can get everything, every param it needs.
If it need get param that inbedded in button, just use key word "this".
If it need get param that of other controls, just use "document.get***By***();".
相关推荐
EFI驱动模型与Windows驱动模型的比较和Driver Binding的实现 本文主要介绍了EFI驱动模型的概念,特别是Driver Binding的实现过程,同时借鉴了Windows驱动模型的一些概念,以便更好地理解EFI驱动模型。 首先,我们...
TwoWay: Binding from model to view and view to model OneWayToSource: Binding from view to model OneTime: Binding from model to view, and auto release after first emit Simple Binding verticalLayout { ...
在WPF(Windows Presentation Foundation)开发中,Event Binding Markup是一种重要的技术,用于将事件与视图模型中的命令关联,从而实现MVVM(Model-View-ViewModel)设计模式的有效利用。Event Binding Markup ...
<i:EventTrigger EventName="KeyDown"> <i:InvokeCommandAction Command="{Binding KeyDownCommand}" /> </i:EventTrigger> </i:Interaction.Behaviors> ``` 这里的`i:`前缀是指向`System.Windows....
implementation 'androidx.databinding:viewbinding:4.0.1' ``` 3. **生成绑定类**:编译时,Data Binding会自动生成一个绑定类,用于在Java代码中访问和操作布局中的元素。 4. **数据绑定表达式**:例如,`@{...
Introduction 7 Creating custom type instances in XAML 9 Creating a dependency property 15 Using an attached property 25 Creating an attached property 28 Accessing a static property from XAML 33 ...
2. **MVC模式实现**:在基于Model-View-Controller模式的项目中,JGoodies Binding可以作为连接模型和视图的关键组件。 3. **快速原型构建**:对于需要快速搭建UI原型的项目,JGoodies Binding的便捷性和自动化特性...
5. **ValueModel**:这是JGoodies Binding的核心概念之一,它封装了一个可以观察和修改的值。ValueModel可以是简单的Java对象,也可以是复杂的属性结构,通过ValueModel可以方便地实现数据绑定。 6. **Collections ...
在Android应用开发中,MVP(Model-View-Presenter)架构模式是一种常用的设计模式,它有助于保持代码的清晰和可维护性。ViewBinding是Google官方推荐的一种替代findViewById的方法,可以更方便地处理UI元素和业务...
Part I Putting the Domain Model to Work 1 Chapter 1: Crunching Knowledge 7 Chapter 2: Communication and the Use of Language 23 Chapter 3: Binding Model and Implementation 45 Part II Chapter 4: ...
Cannot download linux-x64-72_binding.node/win32-x64-72_binding.node
该类是model和view数值连接的桥梁。 该类的命名规则是基于layout文件名称首字母大写并连接“Binding”后缀产生,例如: activity_getting_started.xml => ActivityGettingStartedBinding 3.activity与layout的连接...
`Command Binding`是WPF中实现这一目标的一种优雅方式,它将视图(View)和视图模型(ViewModel)连接起来,遵循MVVM(Model-View-ViewModel)设计模式。 MVVM模式鼓励将业务逻辑和UI逻辑分离,`Command`对象承载了...
在本文中,我们将深入探讨C#中的MVVM(Model-View-ViewModel)绑定技术,并通过一个名为"BookManage"的示例项目进行演示。MVVM是一种设计模式,它在WPF(Windows Presentation Foundation)和UWP(Universal Windows...
* Data Binding:Data Binding 是一种数据绑定技术,允许开发者在布局文件中使用绑定表达式来关联变量和控件。 * 绑定表达式:绑定表达式是使用“@{}”语法定义的,用于关联变量和控件。 * 变量:变量是指在布局文件...
`DataSetManager().UpdateDataSet(dataSetModels, typeof(ModelData));` `MessageBox.Show("保存成功!");` `}` 通过使用 BindingSource,我们可以轻松地实现 DataSet、DataGridView 和 TextBox 的数据同步更新,...
-OSEK Glossary (位于OSEK Binding 1.4.1,ISO 17356-1的一部分,由ISO-style introduction和glossary组成) -OSEK Binding Specification(基础:OSEK Binding 1.4.1,ISO 17356-2,glossary除外) -OSEK OS(基础...
-OSEK Glossary (位于OSEK Binding 1.4.1,ISO 17356-1的一部分,由ISO-style introduction和glossary组成) -OSEK Binding Specification(基础:OSEK Binding 1.4.1,ISO 17356-2,glossary除外) -OSEK OS(基础...
public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } ...