css 当鼠标悬停在表行上时,如何同时更改行的背景颜色和相应SVG路径的填充颜色?

oyjwcjzk  于 2023-03-20  发布在  其他
关注(0)|答案(1)|浏览(146)

我的网页动态地生成一个表,表行类为tableTr,HTML代码中包含一个SVG图像,路径类为pathClass
使用JavaScript,当鼠标悬停在表格行上时,行的背景颜色以及相应形状的填充颜色应该改变;一旦鼠标移离表格行,两者都恢复正常。
当鼠标悬停在路径上时,路径不应改变填充颜色;这不能反过来起作用。
我完全不知道该用什么方法。我的HTML代码如下:

<table>
    <tr>
        <td>
            <!--the dynamically generated table with the rows 
            that need highlighted-->
            <table id="resultsTable"></table>
        </td>
        <td>
            <object id="map">
                <svg id="mapSvg">
                    <path id="c000000" class="pathClass"></path>        
                </svg>
            </object>
        </td>
    </tr>
</table>

我的动态生成表的JS代码如下所示:

var table = document.createElement("table");
table.setAttribute("class", "resultsTable");
table.setAttribute("id", "resultsTable");
for (var q = 0; q < 3143; q++){
    var tr = document.createElement("tr");
    tr.setAttribute("class", "tableTr");
    var td1 = document.createElement('td');
    td1.setAttribute("class", "tableTd");
    var td2 = document.createElement('td');
    td2.setAttribute("class", "tableTd");
    if (q % 2){
        td1.setAttribute("bgcolor", "#DEEAF6");
        td2.setAttribute("bgcolor", "#DEEAF6");
    } else {
        td1.setAttribute("bgcolor", "#FFFFFF");
        td2.setAttribute("bgcolor", "#FFFFFF");
    }        
    if (q){
        var text1 = document.createTextNode(results[q-1][0]);
        var text2 = document.createTextNode(results[q-1][1]);
        td1.appendChild(text1);
        td2.appendChild(text2);
        tr.appendChild(td1);
        tr.appendChild(td2);
        table.appendChild(tr);
   } else {
       var text1 = document.createTextNode("County");
       var text2 = document.createTextNode("Deviation from Optimal");
        td1.appendChild(text1);
        td2.appendChild(text2);
        tr.appendChild(td1);
        tr.appendChild(td2);
        table.appendChild(tr);
        }
    }
    document.getElementById("resultsCell").appendChild(table);
jtw3ybtb

jtw3ybtb1#

我相信你想达到的效果与下面的效果类似,当鼠标悬停在表格中对应的行上时,Map中的每个国家都会高亮显示,而当鼠标悬停在国家形状(html中的国家路径)上时,它不会高亮显示。

var tableRows = document.querySelectorAll(".results-table tr");

console.log(tableRows);

tableRows.forEach((row) => {
  row.addEventListener("mouseover", colorCountry);
  row.addEventListener("mouseout", resetCountryColor);

})

function colorCountry(e) {
  e.currentTarget.style.background = "lightgray";
  document.querySelector(`#${e.currentTarget.dataset.country}`).style.fill = "green";
}

function resetCountryColor(e) {
  e.currentTarget.style.background = "white";
  document.querySelector(`#${e.currentTarget.dataset.country}`).style.fill = "#f2f2f2";
}
.results-table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
  padding: 0;
}

td,
tr {
  border: 1px solid #333;
  text-align: left;
  margin: 0;
  padding: 0;
}

/* .results-table tr:hover{ 
  background: lightgray;
} */
<table>
  <tr>
    <td>
      <table class="results-table">
        <tr data-country="EG">
          <td>Egypt</td>
        </tr>
        <tr data-country="SD">
          <td>Sudan</td>
        </tr>
        <tr data-country="ER">
          <td>Eritrea</td>
        </tr>
        <tr data-country="SO">
          <td>Somalia</td>
        </tr>
        <tr data-country="ET">
          <td>Ethiopia</td>
        </tr>
        <tr data-country="DJ">
          <td>Djibouti</td>
        </tr>
        <tr data-country="LY">
          <td>Libya</td>
        </tr>
        <tr data-country="SS">
          <td>South Sudan</td>
        </tr>
      </table>
    </td>
  </tr>
  <tr>
    <td>
      <object class="map">
        <svg class="map-svg" height="350px" pretty_print="False" style="stroke-linejoin: round; stroke:#000; fill: none;" version="1.1" viewBox="0 0 1000 1001" width="400px">
          <sodipodi:namedview pagecolor="#ffffff" bordercolor="#666666" borderopacity="1" objecttolerance="10" gridtolerance="10" guidetolerance="10" inkscape:pageopacity="0" inkscape:pageshadow="2" inkscape:window-width="640" inkscape:window-height="480" id="namedview68" showgrid="false" inkscape:zoom="0.33342098" inkscape:cx="603.38628" inkscape:cy="543.55784" inkscape:window-x="1456" inkscape:window-y="314" inkscape:window-maximized="0" inkscape:current-layer="svg2" />
          <defs id="defs4">
            <style type="text/css" id="style6">
              path {
                fill-rule: evenodd;
              }

            </style>
          </defs>
          <metadata id="metadata8">
            <views id="views10">
              <view h="1001" padding="0" w="1000" id="view12">
                <proj flip="auto" id="robinson" lon0="100.0" />
                <bbox h="1210.83" w="1011.29" x="-258.56" y="-626.99" id="bbox15" />
              </view>
            </views>
            <rdf:RDF>
              <cc:Work rdf:about="">
                <dc:format>image/svg+xml</dc:format>
                <dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
              </cc:Work>
            </rdf:RDF>
          </metadata>

          <path class="country" inkscape:connector-curvature="0" id="DJ" data-name="Djibouti" data-id="DJ" d="m 820.3,358.7 -5.3,-3.8 6.8,-3.3 0.1,-5.7 -3,-4.3 -3.6,3.4 -5.2,-1.2 -4,6.1 -3.9,6.5 1.1,3.8 0.3,4.2 6.8,0.2 2.9,-0.9 2.8,2.4 4.2,-7.4 z" style="fill:#f2f2f2" />
          <path class="country" inkscape:connector-curvature="0" id="EG" data-name="Egypt" data-id="EG" d="m 628.8,88.1 -11.9,-4 -11.5,-3.7 -15.6,0.2 -3.7,6.6 2.2,5.9 -2.4,8.5 4.2,11.2 2.9,49.2 2.2,50.9 48.1,0 46.4,0 47.4,0 -2.2,-2.8 -14.7,-12.4 -0.9,-9 2.2,-2.4 -11.6,-15.3 -4.4,-7.9 -4.9,-7.5 -10.5,-21.6 -8.4,-13.9 -6.1,-14.5 1.1,-1.3 10.1,19.8 5.8,6.2 4.3,4.4 2.5,-2.4 2.7,-7.2 1.6,-10.4 2.8,-5.6 -1.5,-3.5 -8.5,-20.1 0,0 -5.4,3.4 -9.2,-0.8 -9.6,-3.2 -2.3,4.5 -3.8,-6.8 -8.5,-1.8 -10.2,1.2 -4.5,3.9 -8.6,4.4 -5.6,-2.2 z" style="fill:#f2f2f2" />
          <path class="country" inkscape:connector-curvature="0" id="ER" data-name="Eritrea" data-id="ER" d="m 777.6,303.8 -7,-6.8 -4,-12.7 -7.8,-16 -5.7,7.9 -8.8,2.3 -3.6,4.2 -0.7,9.2 -4.3,20.3 1.5,5.6 14.3,2.9 3.3,-10.4 7.6,6.3 7,-3.2 3,2.9 8.3,0.1 10.8,5.6 3.4,4.8 5.5,4.4 5.4,8.1 4.3,4.5 5.2,1.2 3.6,-3.4 -6.2,-4.2 -4,-4.7 -6.9,-8 -7.1,-7.9 -17.1,-13 z" style="fill:#f2f2f2" />
          <path class="country" inkscape:connector-curvature="0" id="ET" data-name="Ethiopia" data-id="ET" d="m 754.8,310.3 -3.3,10.4 -14.3,-2.9 -1.4,11.9 -4.5,13.6 -7.1,6.8 -4.8,10.6 -1.1,5.7 -5.6,3.8 -3.3,14.5 0.1,1.7 0.4,10.8 -1.8,4.2 -6.4,0.3 -4,7.9 7.5,1 6.3,6.7 2.2,5.6 5.7,3.2 7.4,15 6.4,2.4 0.1,7.7 4.2,4.6 8.5,0 15.7,11.7 3.8,0.2 2.9,-0.4 2.7,1.6 8.2,1.1 3.5,-5.8 11.1,-5.8 4.9,4.7 8.4,0 3.3,-4.4 7.8,-0.2 10.7,-9.8 15.9,-0.6 33.5,-41.5 -10.3,0.1 -40.3,-16.4 -4.8,-5 -4.6,-6.6 -4.8,-7.7 2.5,-4.9 -2.8,-2.4 -2.9,0.9 -6.8,-0.2 -0.3,-4.2 -1.1,-3.8 3.9,-6.5 4,-6.1 -4.3,-4.5 -5.4,-8.1 -5.5,-4.4 -3.4,-4.8 -10.8,-5.6 -8.3,-0.1 -3,-2.9 -7,3.2 -7.6,-6.3 z" style="fill:#f2f2f2" />
          <path class="country" inkscape:connector-curvature="0" id="LY" data-name="Libya" data-id="LY" d="m 589.8,80.6 -3.1,-4.5 -11.7,-1.6 -3.9,-2.4 -4.4,0 -4.4,-6.2 -15.9,-2.8 -7.9,1.8 -7.9,6.5 -3.3,6.7 3.4,10.6 -5.3,6.3 -5.5,3.6 L 507,91.7 490.3,86 479.7,83.3 473.6,71 l -15.7,-6.1 -9.8,-2.3 -4.8,1.2 -13.8,-4.8 -0.3,10.6 -5.6,4 -3.4,4.4 -7.9,5.3 1.5,5.7 -0.9,5.8 -5.6,3.2 4.1,12.2 0.8,6.4 -1.8,11.3 1,6.3 -1.4,7.6 1.3,8.7 -4.6,5.8 7.2,10.1 0.6,5.9 4.3,7.7 5.5,-2.5 9.6,6.4 5.4,8.7 19,6 6.9,7.5 8.3,-5.1 11.9,-7.6 48.4,26.5 48.7,26.5 -0.1,-5.8 13.8,0 -1,-27.7 -2.2,-50.9 -2.9,-49.2 -4.2,-11.2 2.4,-8.5 -2.2,-5.9 3.7,-6.6 z" style="fill:#f2f2f2" />

          <path class="country" inkscape:connector-curvature="0" id="SD" data-name="Sudan" data-id="SD" d="m 740,291.9 0.7,-9.2 3.6,-4.2 8.8,-2.3 5.7,-7.9 -6.9,-5.1 -4.8,-3.4 -5.3,-16.5 -2.5,-14.3 2.5,-2.5 -4.7,-13.6 -47.4,0 -46.4,0 -48.1,0 1,27.7 -13.8,0 0.1,5.8 2.4,54.9 -10.5,-0.9 -5.2,10.2 -3,8.5 2.6,3.2 -3.8,4.3 1.5,5.7 -3,5.8 -1.2,5 4.3,-0.8 2.6,5.4 0.3,8 4.6,4.1 -0.1,3.4 1.5,5.9 7.2,8.6 0.1,5.7 -1.9,5.7 0.9,4.3 4.2,4 1,0.7 3.8,-1.6 4.1,-2.6 2.9,-12.3 3.2,-6.4 8.8,-1.9 2.1,3.8 6.5,8 3.3,1.2 4.4,-2.3 8.7,0.4 1.8,2.9 12.1,0 0.3,-2.9 6.3,-2.6 1.1,-4 4.6,-2.9 10.4,8.1 6.2,-1.4 5.8,-10 6.5,-7.6 -1.3,-8.3 -3,-4 7.3,-0.8 0.7,-3 5.7,0.9 -1.2,10.2 1.7,10 6.5,5.5 1.5,4.7 0,6.9 1.7,0.3 -0.1,-1.7 3.3,-14.5 5.6,-3.8 1.1,-5.7 4.8,-10.6 7.1,-6.8 4.5,-13.6 1.4,-11.9 -1.5,-5.6 4.3,-20.3 z" style="fill:#f2f2f2" />
          <path class="country" inkscape:connector-curvature="0" id="SS" data-name="S. Sudan" data-id="SS" d="m 707.8,379.2 -1.5,-4.7 -6.5,-5.5 -1.7,-10 1.2,-10.2 -5.7,-0.9 -0.7,3 -7.3,0.8 3,4 1.3,8.3 -6.5,7.6 -5.8,10 -6.2,1.4 -10.4,-8.1 -4.6,2.9 -1.1,4 -6.3,2.6 -0.3,2.9 -12.1,0 -1.8,-2.9 -8.7,-0.4 -4.4,2.3 -3.3,-1.2 -6.5,-8 -2.1,-3.8 -8.8,1.9 -3.2,6.4 -2.9,12.3 -4.1,2.6 -3.8,1.6 8.4,5.4 6.8,5.5 0.1,4.5 8.3,7.2 5.2,6 3.2,8.3 9.2,5.5 2,4.4 7.5,11.4 5.5,1.7 3.2,-2.3 5.7,0.9 6.8,-2.9 2.9,5.9 10.9,9.2 0,0 5,-3.8 7.8,3.1 9.8,-3.3 8.6,0.1 7.4,-6.4 7.4,-8.3 8.1,-9.1 -7.4,-15 -5.7,-3.2 -2.2,-5.6 -6.3,-6.7 -7.5,-1 4,-7.9 6.4,-0.3 1.8,-4.2 -0.4,-10.8 -1.7,-0.3 0,-6.9 z" style="fill:#f2f2f2" />

          <path class="country" inkscape:connector-curvature="0" id="SO" data-name="Somalia" data-id="SO" d="m 832.6,372.8 -5.7,-5.8 -2.5,-5.7 -4.1,-2.6 -4.2,7.4 -2.5,4.9 4.8,7.7 4.6,6.6 4.8,5 40.3,16.4 10.3,-0.1 -33.5,41.5 -15.9,0.6 -10.7,9.8 -7.8,0.2 -3.3,4.4 -10.5,15.7 0.4,50.4 7.2,11.4 2.7,-3.3 2.9,-7.3 13.4,-16.7 11.4,-10.6 18.1,-13.7 12.1,-11.2 14.1,-18.9 10.1,-15.5 10,-20.2 7,-17.7 5.4,-15.5 2.9,-14.9 2.4,-5 -0.4,-7.3 0.8,-8 -0.5,-3.9 -4.6,0.1 -5.5,4.7 -6.4,1.4 -5.5,2.1 -3.9,0.2 0,0 -6.9,0.5 -4.2,2.6 -6,0.9 -10.5,4.3 -13.2,1.6 -11.3,3.5 -6.1,0 z" style="fill:#f2f2f2" />
        </svg>
      </object>
    </td>
  </tr>
</table>

相关问题