SVG与html的交互(svg的js与html的js互调用)
2010-12-06 17:54
330 查看
这个例子显示了,在html中单击命令按钮设定svg中的矩形的填充颜色,并且调用svg的js函数FunCallByHtmlJs,产生个消息框。
在svg中,单击矩形时,设置html中的text的文本内容,并且调用html的js函数FunCallBySvgJs,产生个消息框。
svg文档以嵌入在html文档中运行。
例子在IE 6.0 + Adobe SVG Viewer 3.03中文版下测试通过。
svg文件的代码:
//文件名:Svg&HtmlInteractive.svg
html文件的代码:
//文件名:Svg&HtmlInteractive.html
效果图:
在svg中,单击矩形时,设置html中的text的文本内容,并且调用html的js函数FunCallBySvgJs,产生个消息框。
svg文档以嵌入在html文档中运行。
例子在IE 6.0 + Adobe SVG Viewer 3.03中文版下测试通过。
svg文件的代码:
//文件名:Svg&HtmlInteractive.svg
<?xml version="1.0" encoding="utf-8" standalone="no" ?> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="100%" onload="init(evt)" onclick="Click(evt)"> <mce:script type="text/javascript"><!-- var svgDoc = null ; var svgRoot = null ; var parentWnd = null ; //保存html的window对象 //初始化 function init(evt) { svgDoc = evt.target.ownerDocument ; svgRoot = svgDoc.documentElement ; //在html中的第二种交互方式会用到 parentWnd = window.parent ; //ASV 3.0可以这么写。英文6.0版的要换种写法 if(parentWnd.document.title == null || parentWnd.document.title == '') { alert("请不要直接在浏览器中打开‘svg’文档!"); //下面的代码作用是不提示确认关闭窗口 parentWnd.opener = null ; parentWnd.open('', '_self') ; parentWnd.close() ; } svgDoc.svgWnd = window ; //这里人为进行设定,以便在html中的第一种交互方式中可以取的到svg的window对象 } function FunCallByHtmlJs() { alert('这个消息框是在html的js中调用svg的js函数产生的。') ; } function Click(evt) { var id = evt.target.id ; if(id == 'rect') //单击在矩形上,而不是背景上时 { if(parentWnd) { parentWnd.txt.value = '在svg中设置html中的text的文本内容' ; parentWnd.FunCallBySvgJs() ; //调用html中的js函数 } } } // --></mce:script> <rect id="background" x="0" y="0" width="100%" height="100%" fill="gray" /> <rect id="rect" x="50" y="50" width="100" height="100" fill="green" /> <text font-family="SimSun" font-size="14" fill="yellow" x="50" y="50" id="text">单击svg的矩形,设置html的text文本内容</text> </svg>
html文件的代码:
//文件名:Svg&HtmlInteractive.html
<html> <head> <title>SVG与html的交互</title> </head> <body onload="htmInit()"> <mce:script type=text/javascript><!-- var svgDoc = null ; var svgRoot = null ; var svgWnd = null ; //svg的window对象 function htmInit() { txt.value = '' ; } function FunCallBySvgJs() { alert('这个消息框是在svg的js中调用html的js函数产生的。') ; } function Btn1Clk() { //第一种方式 svgDoc = emSvg.getSVGDocument() ; if(svgDoc == null) return ; svgRoot = svgDoc.documentElement ; if(svgRoot == null) return ; var rect = svgRoot.getElementById('rect') ; if(rect) rect.setAttribute('fill', 'blue') ; svgWnd = svgDoc.svgWnd ; //这个window对象是在svg的初始化里面添加进去的 if(svgWnd) svgWnd.FunCallByHtmlJs() ; //调用svg里的js函数 } function Btn2Clk() { //第二种方式 svgWnd = emSvg.window ; if(svgWnd == null) return ; svgRoot = svgWnd.svgRoot ; //svgRoot在svg的js中是个全局的变量 if(svgRoot == null) return ; var rect = svgRoot.getElementById('rect') ; if(rect) rect.setAttribute('fill', 'red') ; svgWnd.FunCallByHtmlJs() ; //调用svg里的js函数 } // --></mce:script> <input type="button" value="设置svg中矩形的填充颜色为蓝色" onclick="Btn1Clk()" /> <input type="button" value="设置svg中矩形的填充颜色为红色" onclick="Btn2Clk()" /> <input id="txt" type="text" value="" /> <embed id="emSvg" src="Svg&HtmlInteractive.svg" mce_src="Svg&HtmlInteractive.svg" width="100%" height="95%" wmode="transparent"/> </body> </html>
效果图:
相关文章推荐
- SVG与html的交互(svg的js与html的js互调用)
- SVG与html的交互(svg的js与html的js互调用)
- 前台js与后台C#互相调用 & JS于HTML交互
- Android实现js及webview交互之在html页面中调用系统摄像头
- 总结Silverlight与HTML页面交互基本方法(js调用Silverlight方法/属性)
- flex与html交互和js调用
- flex与html交互和js调用
- flex与html交互和js调用
- Android 与JS交互 调用本地相册在Html页面显示图片
- iOS之oc与html之间的交互(oc中调用js的方法)
- flex与html交互和js调用
- C#winForm程序与html JS交互调用
- 总结Silverlight与HTML页面交互基本方法(js调用Silverlight方法/属性 和Silverligth调用页面js方法)
- Android实现js及webview交互之在html页面中调用系统摄像头
- Android实现js及webview交互之在html页面中调用系统摄像头
- Android Html交互_二_ JS与App互相调用
- android 本地html JS与android 交互
- HTML 调用 打印机打印 By jQuery.print.js
- asp.net(C#)调用C++程序并交互操作【转】http://www.cnblogs.com/greatverve/archive/2010/06/07/Csharp-transfer-Cpp.html
- html中用js调用ASP文件 实现静态页面动态显示