ersten Beitrag hier. Ich bin mir sicher, dass es eine einfache Lösung dafür gibt, aber ich habe wirklich Probleme damit.Wie ändert man die Farbe von verschiedenen Rechtecken innerhalb eines SVG-Bildes, je nachdem, worauf geklickt wird?
Ich versuche, die Farbe bestimmter Rechtecke innerhalb einer SVG zu aktualisieren, wenn Sie darauf klicken, und ich habe Probleme, die Farbe des angegebenen Rechtecks z. Ich habe ein Bild von einer Karte von Räumen, klicken auf einen einzelnen Raum wird die Farbe des Raumes ändern.
Ich generierte das Svg-Bild mit Illustrator.
HTML:
<xml version="1.0" encoding="utf-8" ?>
<!-- Generator: Adobe Illustrator 21.0.2, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" id="FloorPlan" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 1366 768" style="enable-background:new 0 0 1366 768;" xml:space="preserve" onclick="javascript: changeColour();">
<style type="text/css">
.st0 {
fill: none;
stroke: #000000;
stroke-miterlimit: 10;
}
.st1 {
font-family: 'Myriad-Web';
}
.st2 {
font-size: 19.1885px;
}
</style>
<rect x="98.5" y="153" class="st0" width="1170.5" height="308.6" />
<rect id="Violet" x="99.2" y="182" class="st0" width="81.4" height="76.8"/>
<rect id="Cacoa" x="99.2" y="259.3" class="st0" width="81.4" height="76.8" />
<rect id="Vine" x="99.2" y="336.1" class="st0" width="81.4" height="126.3" />
<rect id="Olive" x="180.7" y="368.1" class="st0" width="81.4" height="94.3" />
<rect id="Cotton" x="262.1" y="368.1" class="st0" width="144.9" height="94.3" />
<rect id="RoseBay" x="407" y="368.1" class="st0" width="144.9" height="94.3" />
<rect id="Clove" x="552" y="368.1" class="st0" width="81.4" height="94.3" />
<rect id="Orchid" x="633.4" y="368.1" class="st0" width="81.4" height="94.3" />
<rect id="Lotus" x="848.7" y="368.1" class="st0" width="81.4" height="94.3" />
<rect id="Bamboo" x="848.7" y="273.7" class="st0" width="81.4" height="94.3" />
<rect id="Spruce" x="1091.6" y="368.1" class="st0" width="81.4" height="94.3" />
<rect id="Pecan" x="1091.6" y="273.7" class="st0" width="81.4" height="94.3" />
<text transform="matrix(0.8632 0 0 1 21.0381 220.3613)" class="st1 st2">Violet (4)</text>
<text transform="matrix(0.8632 0 0 1 20.3779 297.5322)" class="st1 st2">Cacoa (4)</text>
<text transform="matrix(0.8632 0 0 1 21.0967 392.4932)" class="st1 st2">Vine (6)</text>
<text transform="matrix(0.8632 0 0 1 191.2188 502.4238)" class="st1 st2">Olive (4)</text>
<text transform="matrix(0.8632 0 0 1 288.6143 502.3027)" class="st1 st2">Cotton (10)</text>
<text transform="matrix(0.8632 0 0 1 430.7705 501.9131)" class="st1 st2">RoseBay (10)</text>
<text transform="matrix(0.8632 0 0 1 557.7559 501.6709)" class="st1 st2">Clove (4)</text>
<text transform="matrix(0.8632 0 0 1 647.6934 503.2031)" class="st1 st2">Orchid (4)</text>
<text transform="matrix(0.8632 0 0 1 776.0117 424.3525)" class="st1 st2">Lotus (6)</text>
<text transform="matrix(0.8632 0 0 1 747.0732 320.376)" class="st1 st2">Bamboo (6)</text>
<text transform="matrix(0.8632 0 0 1 1006.8291 424.0977)" class="st1 st2">Spruce (4)</text>
<text transform="matrix(0.8632 0 0 1 1011.3525 320.8867)" class="st1 st2">Pecan (4)</text>
</svg>
JavaScript:
<script type="text/javascript">
function changeColour() {
var a = document.getElementById("FloorPlan");
var svgDoc = a.getElementsByTagName('rect');
var svgItem = //notsure how to get the clicked rectangle here
svgItem.setAttribute("fill", "lime");
}
</script>
Haben Sie versucht, ein Klickereignis an jedes rect zu binden? Etwas wie $ rect.click (function() {/ * ändere die Farbe von $ wasauchimmerItem * /}) – GMaiolo