注意,留意这个按钮是如何设置由test类声明的testInstance object的属性的.通常会犯一个错误,即在写LZX方法是在其方法名后加大括号,这是没有必要的。
运行此实例,按下按钮将会显示出如图3所示

图3 事件激活实例
使用脚本代替方法
<canvas width="800" debug="true">
<debug x="450" y="0" height="300"/>
<!-- The previous respond method -->
<method name="respondToButton" >
//Prepare a string
var somestring =
"Hey, I am setting the a1 attributes value";
//Write a debug to see it
Debug.write(somestring);
//Call the alert to see it
showAlert(somestring);
//Go ahead and set the attribute
canvas.testInstance.setAttribute("a1",'satya');
//The above will fire an event causing
//"ona1Method" to execute.
//You will see this in the debugger
</method>
<!-- Your method rewritten as a script -->
<script>
function respondToButtonUsingScript()
{
var somestring =
"Hey, I am setting the a1 attributes value";
Debug.write(somestring);
showAlert(somestring);
canvas.testInstance.setAttribute("a1",'satya');
}
</script>
<!-- Button now calling the global script function -->
<button onclick="respondToButtonUsingScript()">
Show Alert
</button>
<class name="test">
<attribute name="a1" type="string"/>
<attribute name="b1"/>
</class>
<!--Instantiating a class-->
<test name="testInstance">
<!--Demonstrating an onchange for attribute event-->
<method event="ona1" name="ona1Method">
Debug.write("hey this works");
</method>
</test>
<!-- Simulating an alert -->
<!-- A modal alert dialog -->
<alert name="myalert" width="100" y="100">
hi - initial text
</alert>
<!-- A method to work the modal dialog -->
<method name="showAlert" args="displayText">
canvas.myalert.setAttribute("text",displayText);
canvas.myalert.open();
</method>
</canvas>
