我可以使用内部JavaScript来循环遍历SVG元素,并添加或删除类吗?

huangapple go评论91阅读模式
英文:

Can I use internal JavaScript to cycle through elements of an SVG, adding and removing a class?

问题

我有一个带有左右箭头和一个矩形的选择小部件的SVG图像(请参见下面的简化代码片段,其中为简洁起见,将组减少为仅包含数字和彩色矩形)。我想使这个SVG图像具有交互性,以便查看者可以单击箭头来循环浏览可用的组,在两端循环回到开头。所有内容都需要在<svg></svg>标签内部;我不能使用外部HTML或脚本。SVG文件将以<object>标签的形式添加到HTML网页中,以保留交互性。

我已经找到了如何将箭头变成锚元素,并在单击时调用JavaScript函数,但我不确定如何使左右函数更改可见的数字。目前,我将所有数字/颜色对作为具有公共类cls-security的SVG组,并为特定数字使用唯一类net-0net-8。所有具有类cls-security的组都使用visibility: hidden进行样式设置,但是还具有类on的组将使用visibility: visible覆盖该样式。我的想法是cycleLeft()函数应该识别具有类cls-security和类on的元素,从该元素中删除类on,转到具有类cls-security的上一个元素**(如果需要,循环到末尾)**,并将类on添加到该元素;cycleRight()函数应该做相同的事情,但是移动到具有类cls-security的下一个元素而不是具有该类的上一个元素。然而,我在如何实现这一点方面遇到了困难。

如果有更好的方法来实现我想要的效果,上述段落中讨论的所有内容都可以更改。

我走在正确的轨道上吗?有更好的方法来实现我想要的效果吗?

<!-- begin snippet: html hide: false console: true babel: false -->
<!-- language: lang-html -->
<svg id="Object-Panel-Security" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="132" height="72" viewBox="0 0 132 72">
    <defs>
        <style>
            .cls-txt-security {
                font-size: 16px;
                font-family: Arial, Helvetica, sans-serif;
                text-anchor: middle;
            }
            .cls-btn-security {
                opacity: 0;
            }
            a:active > .cls-btn-security {
                opacity: 1;
            }
            .cls-security {
                visibility: hidden;
            }
            .cls-security.on {
                visibility: visible;
            }
        </style>
    </defs>
    <g id="Object-Panel-Security-BG">
        <rect x="0" width="132" height="72"/>
        <rect x="4" y="4" width="124" height="64" fill="#dbcad1"/>
        <polygon points="27 12 27 27 31 27 31 45 27 45 27 60 25.875 60 12 36.125 12 35.875 25.875 12 27 12"/>
        <polygon points="105 12 105 27 101 27 101 45 105 45 105 60 106.125 60 120 36.125 120 35.875 106.125 12 105 12"/>
    </g>
    <text id="Object-Panel-Security-Title" class="cls-txt-security" transform="translate(66 27)">Sec Net</text>
    <a href="javascript:void(0)" onclick="cycleLeft()" id="Object-Panel-Security-Cycle-Left">
        <rect x="12" y="12" width="19" height="48" fill="#bebebe" opacity="0"/>
        <polygon points="25 18 25 29 29 29 29 43 25 43 25 54 14.5 36 25 18" fill="#fff" class="cls-btn-security"/>
    </a>
    <a href="javascript:void(0)" onclick="cycleRight()" id="Object-Panel-Security-Cycle-Right">
        <rect x="101" y="12" width="19" height="48" fill="#bebebe" opacity="0"/>
        <polygon points="107 18 107 29 103 29 103 43 107 43 107 54 117.5 36 107 18" fill="#fff" class="cls-btn-security"/>
    </a>
    <g id="Object-Panel-Security-Network-X" class="cls-security net-0">
        <rect x="35" y="34" width="62" height="22" fill="#a6a6a6"/>
        <text class="cls-txt-security" transform="translate(66 51)">X</text>
    </g>
    <g id="Object-Panel-Security-Network-1" class="cls-security net-1 on">
        <rect x="35" y="34" width="62" height="22" fill="#ca0000"/>
        <text class="cls-txt-security" transform="translate(66 51)">1</text>
    </g>
    <g id="Object-Panel-Security-Network-2" class="cls-security net-2">
        <rect x="35" y="34" width="62" height="22" fill="#7cb400"/>
        <text class="cls-txt-security" transform="translate(66 51)">2</text>
    </g>
    <g id="Object-Panel-Security-Network-3" class="cls-security net-3">
        <rect x="35" y="34" width="62" height="22" fill="#005eca"/>
        <text class="cls-txt-security" transform="translate(66 51)">3</text>
    </g>
    <g id="Object-Panel-Security-Network-4" class="cls-security net-4">
        <rect x="35" y="34" width="62" height="22" fill="#e6ba08"/>
        <text class="cls-txt-security" transform="translate(66 51)">4</text>
    </g>
    <g id="Object-Panel-Security-Network-5" class="cls-security net-5">
        <rect x="35" y="34" width="62" height="22" fill="#00a1b9"/>
        <text class="cls-txt-security" transform="translate(66 51)">5</text>
    </g>
    <g id="Object-Panel-Security-Network-6" class="cls-security net-6">
        <rect x="35" y="34" width="62" height="22" fill="#ca00b9"/>
        <text class="cls-txt-security" transform="translate(66 51)">6</text>
    </g>
    <g id="Object-Panel-Security-Network-7" class="cls-security net-7">
        <rect x="35" y="34" width="62" height="22" fill="#de7f00"/>
        <text class="cls-txt-security" transform="translate(66 51)">7</text>
    </g>
    <g id="Object-Panel-Security-Network-8" class="cls-security net-8">
        <rect x="35" y="34" width="62" height="22" fill="#00ca94"/>
        <text class="cls-txt-security" transform="translate(66 51)">8</text>
    </g>
    <script>//<![CDATA[
        function cycleLeft() {
            return false;
        }
        function cycleRight() {
            return false;
        }
    //]]></script>
</svg>
<!-- end snippet -->
英文:

I have an SVG image of a selection widget with left and right arrows and a rectangle showing a selected group of objects (see simplified code snippet below, where groups have been reduced to just a number and colored rectangle for conciseness). I want to make this SVG image interactive, so the viewer can click the arrows to cycle through the available groups, wrapping around at the ends. Everything needs to be within the &lt;svg&gt;&lt;/svg&gt; tags; I can't use external HTML or scripts. The SVG file will be added to an HTML webpage in an &lt;object&gt; tag to preserve interactivity.

I figured out how to make the arrows into anchor elements that light up and call a JavaScript function when clicked, but I'm not sure how to make the left and right functions change which number is visible. Currently, I have all the number/color pairs as svg groups with a common class, cls-security, and a unique class for the specific number, net-0 through net-8. All groups with the class cls-security are styled with visibility: hidden, but the group which also has the class on overrides that with visibility: visible. My thought is that the cycleLeft() function should identify which element with the class cls-security also has the class on, remove the class on from that element, go to the previous element with the class cls-security (looping around to the end if necessary), and add the class on to that element; and the cycleRight() function should do the same thing but move to the next element with the class cls-security instead of the previous element with that class. However, I'm struggling to figure out how to implement that.

Everything discussed in the above paragraph could be changed if there is a better way to go about getting the effect that I want.

Am I on the right track? Is there a better way to go about doing what I want?

<!-- begin snippet: html hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;svg id=&quot;Object-Panel-Security&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot; width=&quot;132&quot; height=&quot;72&quot; viewBox=&quot;0 0 132 72&quot;&gt;
&lt;defs&gt;
&lt;style&gt;
.cls-txt-security {
font-size: 16px;
font-family: Arial, Helvetica, sans-serif;
text-anchor: middle;
}
.cls-btn-security {
opacity: 0;
}
a:active &gt; .cls-btn-security {
opacity: 1;
}
.cls-security {
visibility: hidden;
}
.cls-security.on {
visibility: visible;
}
&lt;/style&gt;
&lt;/defs&gt;
&lt;g id=&quot;Object-Panel-Security-BG&quot;&gt;
&lt;rect x=&quot;0&quot; width=&quot;132&quot; height=&quot;72&quot;/&gt;
&lt;rect x=&quot;4&quot; y=&quot;4&quot; width=&quot;124&quot; height=&quot;64&quot; fill=&quot;#dbcad1&quot;/&gt;
&lt;polygon points=&quot;27 12 27 27 31 27 31 45 27 45 27 60 25.875 60 12 36.125 12 35.875 25.875 12 27 12&quot;/&gt;
&lt;polygon points=&quot;105 12 105 27 101 27 101 45 105 45 105 60 106.125 60 120 36.125 120 35.875 106.125 12 105 12&quot;/&gt;
&lt;/g&gt;
&lt;text id=&quot;Object-Panel-Security-Title&quot; class=&quot;cls-txt-security&quot; transform=&quot;translate(66 27)&quot;&gt;Sec Net&lt;/text&gt;
&lt;a href=&quot;javascript:void(0)&quot; onclick=&quot;cycleLeft()&quot; id=&quot;Object-Panel-Security-Cycle-Left&quot;&gt;
&lt;rect x=&quot;12&quot; y=&quot;12&quot; width=&quot;19&quot; height=&quot;48&quot; fill=&quot;#bebebe&quot; opacity=&quot;0&quot;/&gt;
&lt;polygon points=&quot;25 18 25 29 29 29 29 43 25 43 25 54 14.5 36 25 18&quot; fill=&quot;#fff&quot; class=&quot;cls-btn-security&quot;/&gt;
&lt;/a&gt;
&lt;a href=&quot;javascript:void(0)&quot; onclick=&quot;cycleRight()&quot; id=&quot;Object-Panel-Security-Cycle-Right&quot;&gt;
&lt;rect x=&quot;101&quot; y=&quot;12&quot; width=&quot;19&quot; height=&quot;48&quot; fill=&quot;#bebebe&quot; opacity=&quot;0&quot;/&gt;
&lt;polygon points=&quot;107 18 107 29 103 29 103 43 107 43 107 54 117.5 36 107 18&quot; fill=&quot;#fff&quot; class=&quot;cls-btn-security&quot;/&gt;
&lt;/a&gt;
&lt;g id=&quot;Object-Panel-Security-Network-X&quot; class=&quot;cls-security net-0&quot;&gt;
&lt;rect x=&quot;35&quot; y=&quot;34&quot; width=&quot;62&quot; height=&quot;22&quot; fill=&quot;#a6a6a6&quot;/&gt;
&lt;text class=&quot;cls-txt-security&quot; transform=&quot;translate(66 51)&quot;&gt;X&lt;/text&gt;
&lt;/g&gt;
&lt;g id=&quot;Object-Panel-Security-Network-1&quot; class=&quot;cls-security net-1 on&quot;&gt;
&lt;rect x=&quot;35&quot; y=&quot;34&quot; width=&quot;62&quot; height=&quot;22&quot; fill=&quot;#ca0000&quot;/&gt;
&lt;text class=&quot;cls-txt-security&quot; transform=&quot;translate(66 51)&quot;&gt;1&lt;/text&gt;
&lt;/g&gt;
&lt;g id=&quot;Object-Panel-Security-Network-2&quot; class=&quot;cls-security net-2&quot;&gt;
&lt;rect x=&quot;35&quot; y=&quot;34&quot; width=&quot;62&quot; height=&quot;22&quot; fill=&quot;#7cb400&quot;/&gt;
&lt;text class=&quot;cls-txt-security&quot; transform=&quot;translate(66 51)&quot;&gt;2&lt;/text&gt;
&lt;/g&gt;
&lt;g id=&quot;Object-Panel-Security-Network-3&quot; class=&quot;cls-security net-3&quot;&gt;
&lt;rect x=&quot;35&quot; y=&quot;34&quot; width=&quot;62&quot; height=&quot;22&quot; fill=&quot;#005eca&quot;/&gt;
&lt;text class=&quot;cls-txt-security&quot; transform=&quot;translate(66 51)&quot;&gt;3&lt;/text&gt;
&lt;/g&gt;
&lt;g id=&quot;Object-Panel-Security-Network-4&quot; class=&quot;cls-security net-4&quot;&gt;
&lt;rect x=&quot;35&quot; y=&quot;34&quot; width=&quot;62&quot; height=&quot;22&quot; fill=&quot;#e6ba08&quot;/&gt;
&lt;text class=&quot;cls-txt-security&quot; transform=&quot;translate(66 51)&quot;&gt;4&lt;/text&gt;
&lt;/g&gt;
&lt;g id=&quot;Object-Panel-Security-Network-5&quot; class=&quot;cls-security net-5&quot;&gt;
&lt;rect x=&quot;35&quot; y=&quot;34&quot; width=&quot;62&quot; height=&quot;22&quot; fill=&quot;#00a1b9&quot;/&gt;
&lt;text class=&quot;cls-txt-security&quot; transform=&quot;translate(66 51)&quot;&gt;5&lt;/text&gt;
&lt;/g&gt;
&lt;g id=&quot;Object-Panel-Security-Network-6&quot; class=&quot;cls-security net-6&quot;&gt;
&lt;rect x=&quot;35&quot; y=&quot;34&quot; width=&quot;62&quot; height=&quot;22&quot; fill=&quot;#ca00b9&quot;/&gt;
&lt;text class=&quot;cls-txt-security&quot; transform=&quot;translate(66 51)&quot;&gt;6&lt;/text&gt;
&lt;/g&gt;
&lt;g id=&quot;Object-Panel-Security-Network-7&quot; class=&quot;cls-security net-7&quot;&gt;
&lt;rect x=&quot;35&quot; y=&quot;34&quot; width=&quot;62&quot; height=&quot;22&quot; fill=&quot;#de7f00&quot;/&gt;
&lt;text class=&quot;cls-txt-security&quot; transform=&quot;translate(66 51)&quot;&gt;7&lt;/text&gt;
&lt;/g&gt;
&lt;g id=&quot;Object-Panel-Security-Network-8&quot; class=&quot;cls-security net-8&quot;&gt;
&lt;rect x=&quot;35&quot; y=&quot;34&quot; width=&quot;62&quot; height=&quot;22&quot; fill=&quot;#00ca94&quot;/&gt;
&lt;text class=&quot;cls-txt-security&quot; transform=&quot;translate(66 51)&quot;&gt;8&lt;/text&gt;
&lt;/g&gt;
&lt;script&gt;//&lt;![CDATA[
function cycleLeft() {
return false;
}
function cycleRight() {
return false;
}
//]]&gt;&lt;/script&gt;
&lt;/svg&gt;

<!-- end snippet -->

答案1

得分: 1

你可以使用查询选择器来获取当前选中的元素,然后对其进行加减操作。

由于你使用了类名"on",你可以切换类名或者添加/移除类名,就像我在下面的示例中所展示的那样。

function cycleLeft() {
  const parent = document.querySelector("svg > .cls-security.on");
  const elementNum = parent.querySelector("text").innerHTML;
  const num = ((parseInt(elementNum) || 0) + 8) % 9;
  parent.classList.remove("on");
  const newElem = document.querySelector(`svg > .cls-security.net-${num}`);
  newElem.classList.add("on");
}

function cycleRight() {
  const parent = document.querySelector("svg > .cls-security.on");
  const elementNum = parent.querySelector("text").innerHTML;
  const num = ((parseInt(elementNum) || 0) + 1) % 9;
  parent.classList.remove("on");
  const newElem = document.querySelector(`svg > .cls-security.net-${num}`);
  newElem.classList.add("on");
}
<svg id="Object-Panel-Security" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="132" height="72" viewBox="0 0 132 72">
    <defs>
        <style>
            .cls-txt-security {
                font-size: 16px;
                font-family: Arial, Helvetica, sans-serif;
                text-anchor: middle;
            }
            .cls-btn-security {
                opacity: 0;
            }
            a:active > .cls-btn-security {
                opacity: 1;
            }
            .cls-security {
                visibility: hidden;
            }
            .cls-security.on {
                visibility: visible;
            }
        </style>
    </defs>
    <g id="Object-Panel-Security-BG">
        <rect x="0" width="132" height="72"/>
        <rect x="4" y="4" width="124" height="64" fill="#dbcad1"/>
        <polygon points="27 12 27 27 31 27 31 45 27 45 27 60 25.875 60 12 36.125 12 35.875 25.875 12 27 12"/>
        <polygon points="105 12 105 27 101 27 101 45 105 45 105 60 106.125 60 120 36.125 120 35.875 106.125 12 105 12"/>
    </g>
    <text id="Object-Panel-Security-Title" class="cls-txt-security" transform="translate(66 27)">Sec Net</text>
    <a href="javascript:void(0)" onclick="cycleLeft()" id="Object-Panel-Security-Cycle-Left">
        <rect x="12" y="12" width="19" height="48" fill="#bebebe" opacity="0"/>
        <polygon points="25 18 25 29 29 29 29 43 25 43 25 54 14.5 36 25 18" fill="#fff" class="cls-btn-security"/>
    </a>
    <a href="javascript:void(0)" onclick="cycleRight()" id="Object-Panel-Security-Cycle-Right">
        <rect x="101" y="12" width="19" height="48" fill="#bebebe" opacity="0"/>
        <polygon points="107 18 107 29 103 29 103 43 107 43 107 54 117.5 36 107 18" fill="#fff" class="cls-btn-security"/>
    </a>
    <g id="Object-Panel-Security-Network-X" class="cls-security net-0">
        <rect x="35" y="34" width="62" height="22" fill="#a6a6a6"/>
        <text class="cls-txt-security" transform="translate(66 51)">X</text>
    </g>
    <g id="Object-Panel-Security-Network-1" class="cls-security net-1 on">
        <rect x="35" y="34" width="62" height="22" fill="#ca0000"/>
        <text class="cls-txt-security" transform="translate(66 51)">1</text>
    </g>
    <g id="Object-Panel-Security-Network-2" class="cls-security net-2">
        <rect x="35" y="34" width="62" height="22" fill="#7cb400"/>
        <text class="cls-txt-security" transform="translate(66 51)">2</text>
    </g>
    <g id="Object-Panel-Security-Network-3" class="cls-security net-3">
        <rect x="35" y="34" width="62" height="22" fill="#005eca"/>
        <text class="cls-txt-security" transform="translate(66 51)">3</text>
    </g>
    <g id="Object-Panel-Security-Network-4" class="cls-security net-4">
        <rect x="35" y="34" width="62" height="22" fill="#e6ba08"/>
        <text class="cls-txt-security" transform="translate(66 51)">4</text>
    </g>
    <g id="Object-Panel-Security-Network-5" class="cls-security net-5">
        <rect x="35" y="34" width="62" height="22" fill="#00a1b9"/>
        <text class="cls-txt-security" transform="translate(66 51)">5</text>
    </g>
    <g id="Object-Panel-Security-Network-6" class="cls-security net-6">
        <rect x="35" y="34" width="62" height="22" fill="#ca00b9"/>
        <text class="cls-txt-security" transform="translate(66 51)">6</text>
    </g>
    <g id="Object-Panel-Security-Network-7" class="cls-security net-7">
        <rect x="35" y="34" width="62" height="22" fill="#de7f00"/>
        <text class="cls-txt-security" transform="translate(66 51)">7</text>
    </g>
    <g id="Object-Panel-Security-Network-8" class="cls-security net-8">
        <rect x="35" y="34" width="62" height="22" fill="#00ca94"/>
        <text class="cls-txt-security" transform="translate(66 51)">8</text>
    </g>
</svg>

希望对你有所帮助!

英文:

You can use query selectors to get the current selected one and then subtract or add a number to it.

Since you use the class "on", you can toggle the class or just add or remove them like I show in my example below.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function cycleLeft() {
const parent = document.querySelector(&quot;svg &gt; .cls-security.on&quot;)
const elementNum = parent.querySelector(&quot;text&quot;).innerHTML
const num =((parseInt(elementNum) || 0) + 8) % 9
parent.classList.remove(&quot;on&quot;)
const newElem = document.querySelector(`svg &gt; .cls-security.net-${num}`)
newElem.classList.add(&quot;on&quot;)
}
function cycleRight() {
const parent = document.querySelector(&quot;svg &gt; .cls-security.on&quot;)
const elementNum = parent.querySelector(&quot;text&quot;).innerHTML
const num = ((parseInt(elementNum) || 0) + 1) % 9
parent.classList.remove(&quot;on&quot;)
const newElem = document.querySelector(`svg &gt; .cls-security.net-${num}`)
newElem.classList.add(&quot;on&quot;)
}

<!-- language: lang-html -->

&lt;svg id=&quot;Object-Panel-Security&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot; width=&quot;132&quot; height=&quot;72&quot; viewBox=&quot;0 0 132 72&quot;&gt;
&lt;defs&gt;
&lt;style&gt;
.cls-txt-security {
font-size: 16px;
font-family: Arial, Helvetica, sans-serif;
text-anchor: middle;
}
.cls-btn-security {
opacity: 0;
}
a:active &gt; .cls-btn-security {
opacity: 1;
}
.cls-security {
visibility: hidden;
}
.cls-security.on {
visibility: visible;
}
&lt;/style&gt;
&lt;/defs&gt;
&lt;g id=&quot;Object-Panel-Security-BG&quot;&gt;
&lt;rect x=&quot;0&quot; width=&quot;132&quot; height=&quot;72&quot;/&gt;
&lt;rect x=&quot;4&quot; y=&quot;4&quot; width=&quot;124&quot; height=&quot;64&quot; fill=&quot;#dbcad1&quot;/&gt;
&lt;polygon points=&quot;27 12 27 27 31 27 31 45 27 45 27 60 25.875 60 12 36.125 12 35.875 25.875 12 27 12&quot;/&gt;
&lt;polygon points=&quot;105 12 105 27 101 27 101 45 105 45 105 60 106.125 60 120 36.125 120 35.875 106.125 12 105 12&quot;/&gt;
&lt;/g&gt;
&lt;text id=&quot;Object-Panel-Security-Title&quot; class=&quot;cls-txt-security&quot; transform=&quot;translate(66 27)&quot;&gt;Sec Net&lt;/text&gt;
&lt;a href=&quot;javascript:void(0)&quot; onclick=&quot;cycleLeft()&quot; id=&quot;Object-Panel-Security-Cycle-Left&quot;&gt;
&lt;rect x=&quot;12&quot; y=&quot;12&quot; width=&quot;19&quot; height=&quot;48&quot; fill=&quot;#bebebe&quot; opacity=&quot;0&quot;/&gt;
&lt;polygon points=&quot;25 18 25 29 29 29 29 43 25 43 25 54 14.5 36 25 18&quot; fill=&quot;#fff&quot; class=&quot;cls-btn-security&quot;/&gt;
&lt;/a&gt;
&lt;a href=&quot;javascript:void(0)&quot; onclick=&quot;cycleRight()&quot; id=&quot;Object-Panel-Security-Cycle-Right&quot;&gt;
&lt;rect x=&quot;101&quot; y=&quot;12&quot; width=&quot;19&quot; height=&quot;48&quot; fill=&quot;#bebebe&quot; opacity=&quot;0&quot;/&gt;
&lt;polygon points=&quot;107 18 107 29 103 29 103 43 107 43 107 54 117.5 36 107 18&quot; fill=&quot;#fff&quot; class=&quot;cls-btn-security&quot;/&gt;
&lt;/a&gt;
&lt;g id=&quot;Object-Panel-Security-Network-X&quot; class=&quot;cls-security net-0&quot;&gt;
&lt;rect x=&quot;35&quot; y=&quot;34&quot; width=&quot;62&quot; height=&quot;22&quot; fill=&quot;#a6a6a6&quot;/&gt;
&lt;text class=&quot;cls-txt-security&quot; transform=&quot;translate(66 51)&quot;&gt;X&lt;/text&gt;
&lt;/g&gt;
&lt;g id=&quot;Object-Panel-Security-Network-1&quot; class=&quot;cls-security net-1 on&quot;&gt;
&lt;rect x=&quot;35&quot; y=&quot;34&quot; width=&quot;62&quot; height=&quot;22&quot; fill=&quot;#ca0000&quot;/&gt;
&lt;text class=&quot;cls-txt-security&quot; transform=&quot;translate(66 51)&quot;&gt;1&lt;/text&gt;
&lt;/g&gt;
&lt;g id=&quot;Object-Panel-Security-Network-2&quot; class=&quot;cls-security net-2&quot;&gt;
&lt;rect x=&quot;35&quot; y=&quot;34&quot; width=&quot;62&quot; height=&quot;22&quot; fill=&quot;#7cb400&quot;/&gt;
&lt;text class=&quot;cls-txt-security&quot; transform=&quot;translate(66 51)&quot;&gt;2&lt;/text&gt;
&lt;/g&gt;
&lt;g id=&quot;Object-Panel-Security-Network-3&quot; class=&quot;cls-security net-3&quot;&gt;
&lt;rect x=&quot;35&quot; y=&quot;34&quot; width=&quot;62&quot; height=&quot;22&quot; fill=&quot;#005eca&quot;/&gt;
&lt;text class=&quot;cls-txt-security&quot; transform=&quot;translate(66 51)&quot;&gt;3&lt;/text&gt;
&lt;/g&gt;
&lt;g id=&quot;Object-Panel-Security-Network-4&quot; class=&quot;cls-security net-4&quot;&gt;
&lt;rect x=&quot;35&quot; y=&quot;34&quot; width=&quot;62&quot; height=&quot;22&quot; fill=&quot;#e6ba08&quot;/&gt;
&lt;text class=&quot;cls-txt-security&quot; transform=&quot;translate(66 51)&quot;&gt;4&lt;/text&gt;
&lt;/g&gt;
&lt;g id=&quot;Object-Panel-Security-Network-5&quot; class=&quot;cls-security net-5&quot;&gt;
&lt;rect x=&quot;35&quot; y=&quot;34&quot; width=&quot;62&quot; height=&quot;22&quot; fill=&quot;#00a1b9&quot;/&gt;
&lt;text class=&quot;cls-txt-security&quot; transform=&quot;translate(66 51)&quot;&gt;5&lt;/text&gt;
&lt;/g&gt;
&lt;g id=&quot;Object-Panel-Security-Network-6&quot; class=&quot;cls-security net-6&quot;&gt;
&lt;rect x=&quot;35&quot; y=&quot;34&quot; width=&quot;62&quot; height=&quot;22&quot; fill=&quot;#ca00b9&quot;/&gt;
&lt;text class=&quot;cls-txt-security&quot; transform=&quot;translate(66 51)&quot;&gt;6&lt;/text&gt;
&lt;/g&gt;
&lt;g id=&quot;Object-Panel-Security-Network-7&quot; class=&quot;cls-security net-7&quot;&gt;
&lt;rect x=&quot;35&quot; y=&quot;34&quot; width=&quot;62&quot; height=&quot;22&quot; fill=&quot;#de7f00&quot;/&gt;
&lt;text class=&quot;cls-txt-security&quot; transform=&quot;translate(66 51)&quot;&gt;7&lt;/text&gt;
&lt;/g&gt;
&lt;g id=&quot;Object-Panel-Security-Network-8&quot; class=&quot;cls-security net-8&quot;&gt;
&lt;rect x=&quot;35&quot; y=&quot;34&quot; width=&quot;62&quot; height=&quot;22&quot; fill=&quot;#00ca94&quot;/&gt;
&lt;text class=&quot;cls-txt-security&quot; transform=&quot;translate(66 51)&quot;&gt;8&lt;/text&gt;
&lt;/g&gt;
&lt;/svg&gt;

<!-- end snippet -->


答案2

得分: 1

你可以这样简化:

<svg id="Object-Panel-Security" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="132" height="72" viewBox="0 0 132 72">
  <defs>
    <style>
      .cls-txt-security {
        font-size   : 16px;
        font-family : Arial, Helvetica, sans-serif;
        text-anchor : middle;
      }
      .cls-btn-security {
        opacity : 0;
      }
      a:active > .cls-btn-security {
        opacity : 1;
      }
    </style>
  </defs>
  <g id="Object-Panel-Security-BG">
    <rect x="0" width="132" height="72"/>
    <rect x="4" y="4" width="124" height="64" fill="#dbcad1"/>
    <polygon points="27 12 27 27 31 27 31 45 27 45 27 60 25.875 60 12 36.125 12 35.875 25.875 12 27 12"/>
    <polygon points="105 12 105 27 101 27 101 45 105 45 105 60 106.125 60 120 36.125 120 35.875 106.125 12 105 12"/>
  </g>
  <text id="Object-Panel-Security-Title" class="cls-txt-security" transform="translate(66 27)">Sec Net</text>
  <a href="javascript:void(0)" onclick="cycleLeft()" id="Object-Panel-Security-Cycle-Left">
    <rect x="12" y="12" width="19" height="48" fill="#bebebe" opacity="0"/>
    <polygon points="25 18 25 29 29 29 29 43 25 43 25 54 14.5 36 25 18" fill="#fff" class="cls-btn-security"/>
  </a>
  <a href="javascript:void(0)" onclick="cycleRight()" id="Object-Panel-Security-Cycle-Right">
    <rect x="101" y="12" width="19" height="48" fill="#bebebe" opacity="0"/>
    <polygon points="107 18 107 29 103 29 103 43 107 43 107 54 117.5 36 107 18" fill="#fff" class="cls-btn-security"/>
  </a>
  <g id="Object-Panel-Security-Network" class="cls-security">
    <rect x="35" y="34" width="62" height="22" fill=""/>
    <text class="cls-txt-security" transform="translate(66 51)"></text>
  </g>
  <script>
    var colors =
      [ { ref: 'X', fill:'#a6a6a6' } 
      , { ref: '1', fill:'#ca0000' } 
      , { ref: '2', fill:'#7cb400' } 
      , { ref: '3', fill:'#005eca' } 
      , { ref: '4', fill:'#e6ba08' } 
      , { ref: '5', fill:'#00a1b9' } 
      , { ref: '6', fill:'#ca00b9' } 
      , { ref: '7', fill:'#de7f00' } 
      , { ref: '8', fill:'#00ca94' } 
      ];
    var 
      currentRef    = 1 
    , ObjPanel      = document.querySelector('#Object-Panel-Security-Network')
    , ObjPanel_Rect = document.querySelector('#Object-Panel-Security-Network > rect')
    , ObjPanel_Text = document.querySelector('#Object-Panel-Security-Network > text')
      ;
    setPanel()
      ;
    function setPanel()
      {
      ObjPanel.className = 'cls-security net-' + currentRef
      ObjPanel_Rect.setAttribute('fill', colors[ currentRef ].fill )
      ObjPanel_Text.textContent = colors[ currentRef ].ref
      }
    function cycleLeft() 
      {
      if ( currentRef > 0) currentRef--;
      setPanel();
      return false;
      }
    function cycleRight()
      {
      if ( currentRef < 8) currentRef++;
      setPanel(); 
      return false;
      }
  </script>
</svg>
英文:

You can simplify on this way:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;svg id=&quot;Object-Panel-Security&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot; width=&quot;132&quot; height=&quot;72&quot; viewBox=&quot;0 0 132 72&quot;&gt;
&lt;defs&gt;
&lt;style&gt;
.cls-txt-security {
font-size   : 16px;
font-family : Arial, Helvetica, sans-serif;
text-anchor : middle;
}
.cls-btn-security {
opacity : 0;
}
a:active &gt; .cls-btn-security {
opacity : 1;
}
&lt;/style&gt;
&lt;/defs&gt;
&lt;g id=&quot;Object-Panel-Security-BG&quot;&gt;
&lt;rect x=&quot;0&quot; width=&quot;132&quot; height=&quot;72&quot;/&gt;
&lt;rect x=&quot;4&quot; y=&quot;4&quot; width=&quot;124&quot; height=&quot;64&quot; fill=&quot;#dbcad1&quot;/&gt;
&lt;polygon points=&quot;27 12 27 27 31 27 31 45 27 45 27 60 25.875 60 12 36.125 12 35.875 25.875 12 27 12&quot;/&gt;
&lt;polygon points=&quot;105 12 105 27 101 27 101 45 105 45 105 60 106.125 60 120 36.125 120 35.875 106.125 12 105 12&quot;/&gt;
&lt;/g&gt;
&lt;text id=&quot;Object-Panel-Security-Title&quot; class=&quot;cls-txt-security&quot; transform=&quot;translate(66 27)&quot;&gt;Sec Net&lt;/text&gt;
&lt;a href=&quot;javascript:void(0)&quot; onclick=&quot;cycleLeft()&quot; id=&quot;Object-Panel-Security-Cycle-Left&quot;&gt;
&lt;rect x=&quot;12&quot; y=&quot;12&quot; width=&quot;19&quot; height=&quot;48&quot; fill=&quot;#bebebe&quot; opacity=&quot;0&quot;/&gt;
&lt;polygon points=&quot;25 18 25 29 29 29 29 43 25 43 25 54 14.5 36 25 18&quot; fill=&quot;#fff&quot; class=&quot;cls-btn-security&quot;/&gt;
&lt;/a&gt;
&lt;a href=&quot;javascript:void(0)&quot; onclick=&quot;cycleRight()&quot; id=&quot;Object-Panel-Security-Cycle-Right&quot;&gt;
&lt;rect x=&quot;101&quot; y=&quot;12&quot; width=&quot;19&quot; height=&quot;48&quot; fill=&quot;#bebebe&quot; opacity=&quot;0&quot;/&gt;
&lt;polygon points=&quot;107 18 107 29 103 29 103 43 107 43 107 54 117.5 36 107 18&quot; fill=&quot;#fff&quot; class=&quot;cls-btn-security&quot;/&gt;
&lt;/a&gt;
&lt;g id=&quot;Object-Panel-Security-Network&quot; class=&quot;cls-security&quot;&gt;
&lt;rect x=&quot;35&quot; y=&quot;34&quot; width=&quot;62&quot; height=&quot;22&quot; fill=&quot;&quot; /&gt;
&lt;text class=&quot;cls-txt-security&quot; transform=&quot;translate(66 51)&quot;&gt;&lt;/text&gt;
&lt;/g&gt;
&lt;script&gt;//&lt;![CDATA[
var colors =
[ { ref: &#39;X&#39;, fill:&#39;#a6a6a6&#39; } 
, { ref: &#39;1&#39;, fill:&#39;#ca0000&#39; } 
, { ref: &#39;2&#39;, fill:&#39;#7cb400&#39; } 
, { ref: &#39;3&#39;, fill:&#39;#005eca&#39; } 
, { ref: &#39;4&#39;, fill:&#39;#e6ba08&#39; } 
, { ref: &#39;5&#39;, fill:&#39;#00a1b9&#39; } 
, { ref: &#39;6&#39;, fill:&#39;#ca00b9&#39; } 
, { ref: &#39;7&#39;, fill:&#39;#de7f00&#39; } 
, { ref: &#39;8&#39;, fill:&#39;#00ca94&#39; } 
];
var 
currentRef    = 1 
, ObjPanel      = document.querySelector(&#39;#Object-Panel-Security-Network&#39;)
, ObjPanel_Rect = document.querySelector(&#39;#Object-Panel-Security-Network &gt; rect&#39;)
, ObjPanel_Text = document.querySelector(&#39;#Object-Panel-Security-Network &gt; text&#39;)
;
setPanel()
;
function setPanel()
{
ObjPanel.className = &#39;cls-security net-&#39; + currentRef
ObjPanel_Rect.setAttribute(&#39;fill&#39;, colors[ currentRef ].fill )
ObjPanel_Text.textContent = colors[ currentRef ].ref
}
function cycleLeft() 
{
if ( currentRef &gt; 0) currentRef--;
setPanel();
return false;
}
function cycleRight()
{
if ( currentRef &lt; 8) currentRef++;
setPanel(); 
return false;
}
//]]&gt;&lt;/script&gt;
&lt;/svg&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年8月9日 01:03:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定