单击 SVG 元素时应用 CSS 样式

irm4nt45

我正在尝试为单击的元素设置 CSS 样式。我试图:active :clicked :targed加上班级名称,但这些都没有奏效。:hover效果很好。感谢您的回答。

HTML:

<a (click)="Clicked(14)" class="lankstumas">
    <ellipse
    class="e-image"
       id="path3769-9"
       cx="43.089287"
       cy="103.09822"
       rx="43.845238"
       ry="39.498512"
       style="fill:#ff7f2a;stroke-width:0.26458332" />
    <text
    class="e-text"
       xml:space="preserve"
       style="font-style:normal;font-weight:normal;font-size:10.58333302px;line-height:1.25;font-family:sans-serif;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.26458332"
       x="22.300594"
       y="99.318459"
       id="text3836"><tspan
         sodipodi:role="line"
         id="tspan3834"
         x="22.300594"
         y="99.318459"
         style="stroke-width:0.26458332">LANKS - </tspan><tspan
         sodipodi:role="line"
         x="22.300594"
         y="112.54762"
         style="stroke-width:0.26458332"
         id="tspan3838">TUMAS</tspan></text>
         </a>

CSS:

.lankstumas:hover{
        .e-image{
            transition: .2s fill;
            fill: #FF8504!important; 
        }
        .e.text{
            transition: .2s fill;
            fill:#fff!important;
        }
        }
鲁宁

我正在尝试为单击的元素设置 CSS 样式。

实现这种效果的一种方法是给元素一个HTML5 Custom data-*这样属性:

data-clicked="false"

然后,您可以使用 javascript 来检测对元素的点击并更新属性。

要使用 javascript 检测点击,请使用:

myElement.addEventListener('click', myFunction, false);

要修改属性使用:

myElement.dataset.myAttribute = myValue;

工作示例:

const boxes = document.getElementsByClassName('box');

const showClick = (e) => {
  event.target.dataset.clicked = 'true';
}

for (let i = 0; i < boxes.length; i++) {

  boxes[i].addEventListener('click', showClick, false);


}
.box {
  display: inline-block;
  width: 100px;
  height: 100px;
  margin: 6px;
  background-color: rgb(255, 0, 0);
  cursor: pointer;
}

.box::after {
  content: 'Click me!';
  display: block;
  text-align: center;
  line-height: 100px;
  color: rgb(255, 255, 255);
  font-weight: bold;
}

.box[data-clicked="true"] {
  color: rgb(255, 0, 0);
  background-color: rgb(255, 255, 0);
}

.box[data-clicked="true"]::after {
  content: 'Clicked!';
  color: rgb(255, 0, 0);
}
<div class="box" data-clicked="false"></div>
<div class="box" data-clicked="false"></div>
<div class="box" data-clicked="false"></div>
<div class="box" data-clicked="false"></div>
<div class="box" data-clicked="false"></div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章