以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 SVG/GML/VRML/X3D/XAML 』  (http://bbs.xml.org.cn/list.asp?boardid=21)
----  svg鼠标响应事件的四种方法  (http://bbs.xml.org.cn/dispbbs.asp?boardid=21&rootid=&id=30661)


--  作者:wanghai00
--  发布时间:4/16/2006 2:45:00 PM

--  svg鼠标响应事件的四种方法
鼠标响应事件的四种方法,以click事件为例。

Mouse Events - SMIL

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
    "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink">
    <rect x="5" y="5" width="40" height="40" fill="red">
        <set attributeName="fill" to="blue" begin="click"/>
    </rect>
</svg>

实例演示:http://www.kevlindev.com/tutorials/basics/events/mouse/svg_smil/click.svg

Mouse Events - Attributes

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
    "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd" [
    <!ATTLIST svg
              xmlns:a3 CDATA #IMPLIED
              a3:scriptImplementation CDATA #IMPLIED>
    <!ATTLIST script
              a3:scriptImplementation CDATA #IMPLIED>
]>
<svg xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink"
     xmlns:a3="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/"
     a3:scriptImplementation="Adobe">
    <script type="text/ecmascript" a3:scriptImplementation="Adobe"><![CDATA[
        function changeColor(evt) {
            var rect = evt.target;
            
            rect.setAttributeNS(null, "fill", "purple")
        }
    ]]></script>
    <rect x="5" y="5" width="40" height="40" fill="red"
          onclick="changeColor(evt)"/>
</svg>

实例演示:

[URL=http://www.kevlindev.com/tutorials/basics/events/mouse/svg_js/onclick.svg]http://www.kevlindev.com/tutorials/basics/events/mouse/svg_js/onclick.svg[/URL]

Mouse Events - JavaScript+SMIL

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
    "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd" [
    <!ATTLIST svg
              xmlns:a3 CDATA #IMPLIED
              a3:scriptImplementation CDATA #IMPLIED>
    <!ATTLIST script
              a3:scriptImplementation CDATA #IMPLIED>
]>
<svg onload="makeShape(evt)"
     xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink"
     xmlns:a3="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/"
     a3:scriptImplementation="Adobe">
    <script type="text/ecmascript" a3:scriptImplementation="Adobe"><![CDATA[
        var svgns = "http://www.w3.org/2000/svg";
        function makeShape(evt) {
            if ( window.svgDocument == null )
                svgDocument = evt.target.ownerDocument;
            
            var rect = svgDocument.createElementNS(svgns, "rect");
            rect.setAttributeNS(null, "x", "5");
            rect.setAttributeNS(null, "y", "5");
            rect.setAttributeNS(null, "width", "40");
            rect.setAttributeNS(null, "height", "40");
            rect.setAttributeNS(null, "fill", "green");
            
            var set = svgDocument.createElementNS(svgns, "set");
            set.setAttributeNS(null, "attributeName", "fill");
            set.setAttributeNS(null, "to", "blue");
            set.setAttributeNS(null, "begin", "click");
            
            rect.appendChild(set);
            svgDocument.documentElement.appendChild(rect);
        }
    ]]></script>
</svg>
实例演示:http://www.kevlindev.com/tutorials/basics/events/mouse/js_dom_smil/click_js_smil.svg

Mouse Events - EventListener

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
    "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd" [
    <!ATTLIST svg
              xmlns:a3 CDATA #IMPLIED
              a3:scriptImplementation CDATA #IMPLIED>
    <!ATTLIST script
              a3:scriptImplementation CDATA #IMPLIED>
]>
<svg onload="makeShape(evt)"
     xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink"
     xmlns:a3="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/"
     a3:scriptImplementation="Adobe">
    <script type="text/ecmascript" a3:scriptImplementation="Adobe"><![CDATA[
        var svgns = "http://www.w3.org/2000/svg";
        function makeShape(evt) {
            if ( window.svgDocument == null )
                svgDocument = evt.target.ownerDocument;
            
            var rect = svgDocument.createElementNS(svgns, "rect");
            rect.setAttributeNS(null, "x", 5);
            rect.setAttributeNS(null, "y", 5);
            rect.setAttributeNS(null, "width", 40);
            rect.setAttributeNS(null, "height", 40);
            rect.setAttributeNS(null, "fill", "green");
            
            rect.addEventListener("click", changeColor, false);
            
            svgDocument.documentElement.appendChild(rect);
        }
        
        function changeColor(evt) {
            var target = evt.target;
            target.setAttributeNS(null, "fill", "purple");
        }
    ]]></script>
</svg>

实例演示:http://www.kevlindev.com/tutorials/basics/events/mouse/js_dom/click_js.svg


--  作者:wyb_cn
--  发布时间:8/17/2006 9:10:00 AM

--  
第1种方法和第3种方法在firefox下不能解释
不错不错 正是我想要的
--  作者:qsc800528
--  发布时间:8/2/2007 4:22:00 PM

--  
bucuo
--  作者:mlmzw
--  发布时间:8/11/2007 11:36:00 AM

--  
还蛮不错的,谢谢楼主了
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
60.547ms