batik详解(3)

Scripting with ECMAScript 
脚本基础
作为ECMAScript 语言(JavaScript的标准版本)是最流行的脚本语言之一,并且作为SVG的标准规定任何
一个SVG都必须支持它。SVG documents 处理通过Batik 支持带有使用 Mozilla’s ECMAScript
interpreter, Rhino ECMAScript的脚本.
在SVG 文件中,有两个地方你可以放入脚本。
第一个地方是在script 元素中, 在这里你可以放置任何代码, 包括函数定义, 在document SVGLoad事件
发生之前执行这些脚本.
xml 代码
  1. <svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">     
  2. <script type="text/ecmascript">    // ECMAScript code to be executed   script>  
  3. <!---->svg>  
你也可以帮定脚本来响应用户或者在SVG元素中document事件使用属性。像上边的例子中,脚本语言必须设置
在脚本元素中。无论如何,为了事件处理,默认的语言类型text/ecmascript 被指定. 如果你想改变它,那
么你可以在svg 元素中使用contentScriptType 属性. 脚本属性可以被包含在任意脚本代码中来执行,当事
件延伸到bubbling或者 at-target phases 中时。下面的例子将改变rect被填充为蓝色,当它被点击的时候
xml 代码
  1. <svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">  
  2. <rect x="0" y="0" width="10" height="10" 
  3. onclick="evt.target.setAttribute('fill', 'blue')"/>
  4. svg>  
注意在事件属性脚本内部,这里有一个变量叫做evt,这个变量涉及到表现被处理事件的事件对象。 
要了解更多的在SVG中使用脚本的信息,请参考如下:
   the scripting chapter of the SVG specification, for advanced information on
scripting in SVG, and
   the ECMAScript specification, for advanced information on the ECMAScript language.

你可能感兴趣的:(JavaScript,xml,脚本)