SVG与html的交互(svg的js与html的js互调用)

10 篇文章 0 订阅
4 篇文章 0 订阅

SVGhtml的交互(svgjshtmljs互调用)

这个例子显示了,在html中单击命令按钮设定svg中的矩形的填充颜色,并且调用svgjs函数FunCallByHtmlJs,产生个消息框。

svg中,单击矩形时,设置html中的text的文本内容,并且调用htmljs函数FunCallBySvgJs,产生个消息框。

svg文档以嵌入在html文档中运行。

例子在IE 6.0 + Adobe SVG Viewer 3.03中文版下测试通过。

svg文件的代码:

//文件名:Svg&HtmlInteractive.svg

view plaincopy to clipboardprint?

1.  <?xml version="1.0" encoding="utf-8" standalone="no" ?>  

2.  <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)">  

3.  <mce:script type="text/javascript"><!--  

4.  var svgDoc = null ;  

5.  var svgRoot = null ;  

6.  var parentWnd = null ; //保存htmlwindow对象  

7.  //初始化  

8.  function init(evt)  

9.  {  

10.   svgDoc = evt.target.ownerDocument ;  

11.   svgRoot = svgDoc.documentElement ; //html中的第二种交互方式会用到  

12.   parentWnd = window.parent ; //ASV 3.0可以这么写。英文6.0版的要换种写法  

13.   if(parentWnd.document.title == null || parentWnd.document.title == '')  

14.   {  

15.     alert("请不要直接在浏览器中打开‘svg’文档!");  

16.     //下面的代码作用是不提示确认关闭窗口  

17.     parentWnd.opener = null ;  

18.     parentWnd.open('', '_self') ;  

19.     parentWnd.close() ;  

20.   }  

21.   svgDoc.svgWnd = window ; //这里人为进行设定,以便在html中的第一种交互方式中可以取的到svgwindow对象  

22. }  

23. function FunCallByHtmlJs()  

24. {  

25.   alert('这个消息框是在htmljs中调用svgjs函数产生的。') ;  

26. }  

27. function Click(evt)  

28. {  

29.   var id = evt.target.id ;  

30.   if(id == 'rect') //单击在矩形上,而不是背景上时  

31.   {  

32.     if(parentWnd)  

33.     {  

34.       parentWnd.txt.value = 'svg中设置html中的text的文本内容' ;  

35.       parentWnd.FunCallBySvgJs() ; //调用html中的js函数  

36.     }  

37.   }  

38. }  

39. // --></mce:script>  

40.   <rect id="background" x="0" y="0" width="100%" height="100%" fill="gray" />  

41.   <rect id="rect" x="50" y="50" width="100" height="100" fill="green" />  

42.   <text font-family="SimSun" font-size="14" fill="yellow" x="50" y="50" id="text">单击svg的矩形,设置htmltext文本内容</text>  

43. </svg>  

 

html文件的代码:

//文件名:Svg&HtmlInteractive.html

view plaincopy to clipboardprint?

1.  <html>  

2.  <head>  

3.    <title>SVGhtml的交互</title>  

4.  </head>  

5.  <body onload="htmInit()">  

6.  <mce:script type=text/javascript><!--  

7.  var svgDoc = null ;  

8.  var svgRoot = null ;  

9.  var svgWnd = null ; //svgwindow对象  

10. function htmInit()  

11. {  

12.   txt.value = '' ;  

13. }  

14. function FunCallBySvgJs()  

15. {  

16.   alert('这个消息框是在svgjs中调用htmljs函数产生的。') ;  

17. }  

18. function Btn1Clk()  

19. {  

20.   //第一种方式  

21.   svgDoc = emSvg.getSVGDocument() ;  

22.   if(svgDoc == null) return ;  

23.   svgRoot = svgDoc.documentElement ;  

24.   if(svgRoot == null) return ;  

25.   var rect = svgRoot.getElementById('rect') ;  

26.   if(rect) rect.setAttribute('fill', 'blue') ;  

27.   svgWnd = svgDoc.svgWnd ; //这个window对象是在svg的初始化里面添加进去的  

28.   if(svgWnd) svgWnd.FunCallByHtmlJs() ; //调用svg里的js函数  

29. }  

30. function Btn2Clk()  

31. {  

32.   //第二种方式  

33.   svgWnd = emSvg.window ;  

34.   if(svgWnd == null) return ;  

35.   svgRoot = svgWnd.svgRoot ; //svgRootsvgjs中是个全局的变量  

36.   if(svgRoot == null) return ;  

37.   var rect = svgRoot.getElementById('rect') ;  

38.   if(rect) rect.setAttribute('fill', 'red') ;  

39.   svgWnd.FunCallByHtmlJs() ; //调用svg里的js函数  

40. }  

41. // --></mce:script>  

42. <input type="button" value="设置svg中矩形的填充颜色为蓝色" onclick="Btn1Clk()" />  

43. <input type="button" value="设置svg中矩形的填充颜色为红色" onclick="Btn2Clk()" />  

44. <input id="txt" type="text" value="" />  

45. <embed id="emSvg" src="Svg&HtmlInteractive.svg" mce_src="Svg&HtmlInteractive.svg" width="100%" height="95%" wmode="transparent"/>  

46. </body>  

47. </html>  

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值