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

<!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>

时间: 2024-09-29 10:53:34

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

每天一个JavaScript实例-处理textarea中的字符成每一行

<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>每天一个JavaScript实例-处理textarea中的字符成每一行</title> <script> function clicka(){ console.log("aaa"); var aa = document.get

每天一个JavaScript实例-html5拖拽

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>每天一个JavaScript实例-html5拖拽</title> <style> #drop{ width:300px; height:200px; background-

每天一个JavaScript实例-使用带有定时器的函数闭包

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>每天一个JavaScript实例-使用带有定时器的函数闭包</title> <style> #redbox{ position:absolute; left:100px;

每天一个JavaScript实例-从一个div元素删除一个段落

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>每天一个JavaScript实例-从一个div元素删除一个段落</title> <style> p{ padding:20px; margin:10px 0; width:

每天一个JavaScript实例-判断图片是否加载完成

<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>每天一个JavaScript实例-判断图片是否加载完成</title> <script> window.onload= function(){ //clearTimeout(clock); alert("加载完成"); } v

每天一个JavaScript实例-检測表单数据

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>每天一个JavaScript实例-检測表单数据</title> <style> [role="alert"]{ background-color: #f

每天一个JavaScript实例-确定web页面的区域

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>每天一个JavaScript实例-确定web页面的区域</title> <script> function size(){ var width = 0; var heigh

每天一个JavaScript实例-铺货鼠标点击位置并将元素移动到该位置

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>每天一个JavaScript实例-铺货鼠标点击位置并将元素移动到该位置</title> <style> #info{ width:100px; height:100px;

每天一个JavaScript实例-标量参数和数组参数的功能性区别

<!DOCTYPE> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>每天一个JavaScript实例-标量参数和数组参数的功能性区别</title> <scrip