简体   繁体   English

显示和隐藏SVG中的元素

[英]Show and hide elements inside SVGs

I'm trying to set up an interactive map where you can click on circles (for cities) to make some text appear. 我正在尝试建立一个交互式地图,您可以在其中单击圆圈(对于城市)以显示一些文本。 When you click on another circle another text appears. 当您单击另一个圆圈时,将显示另一个文本。 Here is what I made so far: 这是我到目前为止所做的:

<svg width="320px" height="210px"
     xmlns="http://www.w3.org/2000/svg">
  <title>Javascript und SVG</title>
  <defs>
    <script type="text/javascript">
    <![CDATA[
      var id = 'name';

      function place(id){
        var adress_style = document.getElementById(id).style;
        adress_style.setProperty('display','block');
      }
    ]]>
    </script>

  </defs>

<!-- Text: show and hide -->
<g id="city_1" style="display:none; background-color:white;"><text transform="matrix(1 0 0 1 50 30)"><tspan x="0" y="0" fill="#382787" font-family="'Roboto'" font-size="14">Organisation No. 1</tspan><tspan x="0" y="18" font-family="'Roboto'" font-size="14">Responsible Person</tspan><tspan x="0" y="36" font-family="'Roboto'" font-size="14">Zip City</tspan><tspan x="0" y="54" font-family="'Roboto'" font-size="14">Phone No.</tspan><tspan x="0" y="72" font-family="'Roboto'" font-size="14">mail adress</tspan></text>
</g>
<g id="city_2" style="display:none; background-color:white;"><text transform="matrix(1 0 0 1 50 30)"><tspan x="0" y="0" fill="#382787" font-family="'Roboto'" font-size="14">Another Organisation</tspan><tspan x="0" y="18" font-family="'Roboto'" font-size="14">Another Person</tspan><tspan x="0" y="36" font-family="'Roboto'" font-size="14">Another City</tspan><tspan x="0" y="54" font-family="'Roboto'" font-size="14">Phone No.</tspan><tspan x="0" y="72" font-family="'Roboto'" font-size="14">another mail adress</tspan></text>
</g>
<!-- 2 circles as buttons -->
  <circle cx="240" cy="30" r="10"
    style="fill:white; stroke:black;"
    onclick="place('city_1')" />
  <circle cx="240" cy="70" r="10"
    style="fill:green; stroke:black;"
    onclick="place('city_2')" />

</svg>

It works but the text blocks overlap. 它有效,但文本块重叠。 What can I do so that there is only one text displayed at a time? 我该怎么做才能一次只显示一个文本?

Your help will be appreciated! 您的帮助将不胜感激! Thanks for your help! 谢谢你的帮助!

You could get all the g elements and hide them, then show the one you want. 您可以获取所有g元素并将其隐藏,然后显示所需的元素。

 <svg width="320px" height="210px" xmlns="http://www.w3.org/2000/svg"> <title>Javascript und SVG</title> <defs> <script type="text/javascript"> <![CDATA[ var id = 'name'; function place(id){ var gs = document.getElementsByTagName("g"); for (var i = 0; i < gs.length; i++) { gs[i].style.setProperty('display','none'); } var adress_style = document.getElementById(id).style; adress_style.setProperty('display','block'); } ]]> </script> </defs> <!-- Text: show and hide --> <g id="city_1" style="display:none; background-color:white;"><text transform="matrix(1 0 0 1 50 30)"><tspan x="0" y="0" fill="#382787" font-family="'Roboto'" font-size="14">Organisation No. 1</tspan><tspan x="0" y="18" font-family="'Roboto'" font-size="14">Responsible Person</tspan><tspan x="0" y="36" font-family="'Roboto'" font-size="14">Zip City</tspan><tspan x="0" y="54" font-family="'Roboto'" font-size="14">Phone No.</tspan><tspan x="0" y="72" font-family="'Roboto'" font-size="14">mail adress</tspan></text> </g> <g id="city_2" style="display:none; background-color:white;"><text transform="matrix(1 0 0 1 50 30)"><tspan x="0" y="0" fill="#382787" font-family="'Roboto'" font-size="14">Another Organisation</tspan><tspan x="0" y="18" font-family="'Roboto'" font-size="14">Another Person</tspan><tspan x="0" y="36" font-family="'Roboto'" font-size="14">Another City</tspan><tspan x="0" y="54" font-family="'Roboto'" font-size="14">Phone No.</tspan><tspan x="0" y="72" font-family="'Roboto'" font-size="14">another mail adress</tspan></text> </g> <!-- 2 circles as buttons --> <circle cx="240" cy="30" r="10" style="fill:white; stroke:black;" onclick="place('city_1')" /> <circle cx="240" cy="70" r="10" style="fill:green; stroke:black;" onclick="place('city_2')" /> </svg> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM