如何为SVG图像中的一个
在这种情况下,我想展示非洲,只填充一个从黄色到红色的渐变,但由于子组的存在,填充会产生许多渐变。
Javascript代码:
<script type="text/javascript">
function svgOver() {
var what = $(this).attr("id");
$("#world #"+what, svg.root()).attr("fill", "url(#red_black)");
}
function svgOut() {
$(this).attr("fill", "");
}
...
$("#map").svg({
loadURL: 'http://teszt.privilegetours.hu/skins/privilege/svg/worldmap.svg',
onLoad: function(svg) {
$("#world > g", svg.root()).bind('mouseover', svgOver).bind('mouseout', svgOut).bind('click', svgZoom);
},
settings: {}
});
SVG图像:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" mlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="570px" height="300px" viewBox="146.605 71.42 570 300" enable-background="new 146.605 71.42 570 300" xml:space="preserve">
<defs>
<linearGradient id="red_black" x1="0%" y1="0%" x2="0%" y2="100%">
<stop offset="0%" style="stop-color:rgb(255,0,0);stop-opacity:1"/>
<stop offset="100%" style="stop-color:rgb(255,255,0);stop-opacity:1"/>
</linearGradient>
</defs>
<g id="world" transform="scale(1)" fill="#AAAAAA" stroke="#FFFFFF" stroke-width="0.1">
<g id="africa" name="africa"> // < i want to fill this
<g id="er" transform="translate(-29.9017, -45.0745)"> // < instead of theese
<path d="..."/>
</g>
<g id="yt"> // < instead of theese
<path d="..."/>
</g>
...
如何解决这个问题?
如何在不向原始图像添加另一个<g>
标签的情况下解决这个问题?
#africa
的代码在哪里? - robertc$(“#world> g”,svg.root())。bind('mouseover',svgOver)
。 你真的不能在线链接整个东西吗? - robertc$(“#world> g”,svg.root()).bind('mouseover',svgOver)
绑定mouseover
事件时,将元素的ID传递给svgOver()
函数。因此,svgOver
将等于$(“#world#africa”,svg.root())。attr(“fill”,“url(#red_black)”);
。url(#red_black)
将从svg(<defs>
)中选择。实际上,我没有分享整个网站的权限。稍后我会复制它并分享它。 - RépássvgOver
函数中,你是否尝试过使用 console.log 打印id
属性,以确保你没有收到任何意外的元素? - RussellUresti