用js在svg图上的上动态添加一张任意格式的图片 我想用js在一个指定的svg图上动态的添加一任意格式张图片?有没有哪位大神能够指教一下!!!能够给出代码最好 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 给 svg 添加移除背景就是了 如: background-image: url('/soma.png'); 不是那样的哦,回楼上,我要的是在svg图上的某个点上动态创建一张图片,用了很多方法就是获取不到地图的url地址 SVG的DOM操作?随便写了个,楼主测试下,应该大丈夫。<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> new document </title> <script type="text/javascript"> function init() { var svg_obj = document.getElementById("svg_obj"); svg_obj.onclick = function(event) { var xmlns = "http://www.w3.org/2000/svg"; var tsvg_obj = document.getElementById("svg_obj"); var svg_img = document.createElementNS(xmlns,"image"); svg_img.href.baseVal = "0.png" ; svg_img.setAttributeNS(null,"x",event.pageX); svg_img.setAttributeNS(null,"y",event.pageY); svg_img.setAttributeNS(null,"height","16px"); svg_img.setAttributeNS(null,"width","16px"); tsvg_obj.appendChild(svg_img); } } </script> </head><body onload="init();"><svg id="svg_obj" version="1.1"xmlns="http://www.w3.org/2000/svg" style="width:220px;height:220px;border:1px solid black;"></svg></body></html> 你可以用个<g>标签把点(<circle>标签之内包裹起来作一个节点) svg_img.href.baseVal = "0.png" ;找个图片,重命名为0.png,和网页放在同一目录下。 报错是 这句“var svg_img = document.createElementNS(xmlns, "image");”对象不之处此属性或方法 不会吧,楼主能把测试页面的全代码黏贴出来么?我特意测了下,都没出现楼主的问题。FF下亲测无问题CHROME无问题IE无问题 ie9以下不能支持SVG吧你用的什么浏览器测的? <?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> new document </title> <script type="text/javascript"> function init() { var svg_obj = document.getElementById("svg_obj"); svg_obj.onclick = function(event) { var xmlns = "http://www.w3.org/2000/svg"; var tsvg_obj = document.getElementById("svg_obj"); var svg_img = document.createElementNS(xmlns, "image"); svg_img.href.baseVal = "example.jpg"; svg_img.setAttributeNS(null, "x", event.pageX); svg_img.setAttributeNS(null, "y", event.pageY); svg_img.setAttributeNS(null, "height", "16px"); svg_img.setAttributeNS(null, "width", "16px"); tsvg_obj.appendChild(svg_img); } } </script> </head> <body onload="init();"><svg id="svg_obj" version="1.1"xmlns="http://www.w3.org/2000/svg" style="width:220px;height:220px;border:1px solid black;"></svg></body></html> <?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> new document </title> <script type="text/javascript"> function init() { var svg_obj = document.getElementById("svg_obj"); svg_obj.onclick = function(event) { var xmlns = "http://www.w3.org/2000/svg"; var tsvg_obj = document.getElementById("svg_obj"); var svg_img = document.createElementNS(xmlns, "image"); svg_img.href.baseVal = "example.jpg"; svg_img.setAttributeNS(null, "x", event.pageX); svg_img.setAttributeNS(null, "y", event.pageY); svg_img.setAttributeNS(null, "height", "16px"); svg_img.setAttributeNS(null, "width", "16px"); tsvg_obj.appendChild(svg_img); } } </script> </head> <body onload="init();"><svg id="svg_obj" version="1.1"xmlns="http://www.w3.org/2000/svg" style="width:220px;height:220px;border:1px solid black;"></svg></body></html> <?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> new document </title> <script type="text/javascript"> function init() { var svg_obj = document.getElementById("svg_obj"); svg_obj.onclick = function(event) { var xmlns = "http://www.w3.org/2000/svg"; var tsvg_obj = document.getElementById("svg_obj"); var svg_img = document.createElementNS(xmlns, "image"); svg_img.href.baseVal = "example.jpg"; svg_img.setAttributeNS(null, "x", event.pageX); svg_img.setAttributeNS(null, "y", event.pageY); svg_img.setAttributeNS(null, "height", "16px"); svg_img.setAttributeNS(null, "width", "16px"); tsvg_obj.appendChild(svg_img); } } </script> </head> <body onload="init();"><svg id="svg_obj" version="1.1"xmlns="http://www.w3.org/2000/svg" style="width:220px;height:220px;border:1px solid black;"></svg></body></html>测试了下,没问题。参考9L,IE版本<10的都必须安装SVGViewer,且不能在兼容性视图中预览,Chrome&FF最好升到最新版本试试。 高手帮忙解释下一段菜单js 关于Javascript问题,郁闷! 新手的问题~~热心人快来帮忙啊! 关于firefox和Chrome中form对象的elements属性 关于回车的问题请教 关于脚本中调用window.open()的问题 用了DHTML里的insertCell函数,可以插入多行,可是。。。。。。 问题:F5能屏蔽掉吗? 在线等待 JavaScript的菜鸟提问。 easyui layout 布局问题 JS图片滚动停顿效果为什么只能滚一次就不显示了。求大神,急等!!!!
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<script type="text/javascript">
function init()
{
var svg_obj = document.getElementById("svg_obj");
svg_obj.onclick = function(event)
{
var xmlns = "http://www.w3.org/2000/svg";
var tsvg_obj = document.getElementById("svg_obj");
var svg_img = document.createElementNS(xmlns,"image");
svg_img.href.baseVal = "0.png" ;
svg_img.setAttributeNS(null,"x",event.pageX);
svg_img.setAttributeNS(null,"y",event.pageY);
svg_img.setAttributeNS(null,"height","16px");
svg_img.setAttributeNS(null,"width","16px");
tsvg_obj.appendChild(svg_img);
}
}
</script>
</head><body onload="init();">
<svg id="svg_obj" version="1.1"
xmlns="http://www.w3.org/2000/svg" style="width:220px;height:220px;border:1px solid black;"></svg>
</body>
</html>
找个图片,重命名为0.png,和网页放在同一目录下。
我特意测了下,都没出现楼主的问题。
FF下亲测无问题CHROME无问题IE无问题
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<script type="text/javascript">
function init() {
var svg_obj = document.getElementById("svg_obj");
svg_obj.onclick = function(event) {
var xmlns = "http://www.w3.org/2000/svg";
var tsvg_obj = document.getElementById("svg_obj");
var svg_img = document.createElementNS(xmlns, "image");
svg_img.href.baseVal = "example.jpg";
svg_img.setAttributeNS(null, "x", event.pageX);
svg_img.setAttributeNS(null, "y", event.pageY);
svg_img.setAttributeNS(null, "height", "16px");
svg_img.setAttributeNS(null, "width", "16px");
tsvg_obj.appendChild(svg_img);
}
}
</script>
</head>
<body onload="init();">
<svg id="svg_obj" version="1.1"
xmlns="http://www.w3.org/2000/svg" style="width:220px;height:220px;border:1px solid black;"></svg>
</body>
</html>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<script type="text/javascript">
function init() {
var svg_obj = document.getElementById("svg_obj");
svg_obj.onclick = function(event) {
var xmlns = "http://www.w3.org/2000/svg";
var tsvg_obj = document.getElementById("svg_obj");
var svg_img = document.createElementNS(xmlns, "image");
svg_img.href.baseVal = "example.jpg";
svg_img.setAttributeNS(null, "x", event.pageX);
svg_img.setAttributeNS(null, "y", event.pageY);
svg_img.setAttributeNS(null, "height", "16px");
svg_img.setAttributeNS(null, "width", "16px");
tsvg_obj.appendChild(svg_img);
}
}
</script>
</head>
<body onload="init();">
<svg id="svg_obj" version="1.1"
xmlns="http://www.w3.org/2000/svg" style="width:220px;height:220px;border:1px solid black;"></svg>
</body>
</html>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<script type="text/javascript">
function init() {
var svg_obj = document.getElementById("svg_obj");
svg_obj.onclick = function(event) {
var xmlns = "http://www.w3.org/2000/svg";
var tsvg_obj = document.getElementById("svg_obj");
var svg_img = document.createElementNS(xmlns, "image");
svg_img.href.baseVal = "example.jpg";
svg_img.setAttributeNS(null, "x", event.pageX);
svg_img.setAttributeNS(null, "y", event.pageY);
svg_img.setAttributeNS(null, "height", "16px");
svg_img.setAttributeNS(null, "width", "16px");
tsvg_obj.appendChild(svg_img);
}
}
</script>
</head>
<body onload="init();">
<svg id="svg_obj" version="1.1"
xmlns="http://www.w3.org/2000/svg" style="width:220px;height:220px;border:1px solid black;"></svg>
</body>
</html>
测试了下,没问题。
参考9L,IE版本<10的都必须安装SVGViewer,且不能在兼容性视图中预览,Chrome&FF最好升到最新版本试试。