每天一个JavaScript实例-从js脚本中访问object元素中的SVG

来源:互联网 发布:c语言左移 编辑:程序博客网 时间:2024/04/26 23:15
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>每天一个JavaScript实例-从js脚本中访问object元素中的SVG</title><style></style></head><body><object id="object" data = "每天一个JavaScript实例-SVG中的javaScript展示.svg" style="padding:20px;width:600px;height:600px;"><p>No SVG support</p></object><script type="text/javascript">var object = document.getElementById("object");object.onload = function(){var svgdoc;try {svgdoc = object.contentDocument;}catch(e){try{svgdoc = object.getSVGDocument();}catch(e){alert("SVG in object not support in your environment");}}if(!svgdoc) return;var r = svgdoc.rootElement;var square = svgdoc.getElementById("square");square.onclick = function(){var width = parseFloat(square.getAttributeNS(null,"width"));width -= 50;square.setAttributeNS(null,"width",width);var color = square.getAttributeNS(null,"fill");if(color == "bule"){square.setAttributeNS(null,"fill","yellow");square.setAttributeNS(null,"stroke","green");}else{square.setAttributeNS(null,"fill","bule");square.setAttributeNS(null,"stroke","red");}}}</script></body></html>

0 0
原创粉丝点击