您的位置:首页 > 移动开发 > Objective-C

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

2014-11-03 18:04 761 查看
<!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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: