简体   繁体   English

jQuery 更改类,但 css 不更改

[英]jQuery changes classes but css does not change

I am new at learning how to change classes with JQuery.我是学习如何使用 JQuery 更改课程的新手。 In my example, the hexagons change classes in the DOM, but no CSS is applied.在我的示例中,六边形更改了 DOM 中的类,但没有应用 CSS。 Am I missing something obvious?我错过了一些明显的东西吗?

Thank you for your time!感谢您的时间!

 $('#hexagon1').click(function() { $(this).removeClass("unclicked"); $(this).addClass("clicked"); }); $('#hexagon2').click(function() { $(this).removeClass("unclicked"); $(this).addClass("clicked"); });
 .clicked { fill: #0000FF; }.unclicked { fill: #0000FF; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <svg viewBox="0 0 101.83981 74.999893" id="svg8"> <g id="layer1" transform="translate(-28.970265,-13.579265)"> <path style="opacity:1;fill:#ffb619;stroke-width:4.50001;stroke-linecap:round;stroke-miterlimit:7" id="hexagon1" d="M 43.906288,64.275181 28.970265,39.449215 43.002172,14.101257 71.9701,13.579265 86.906123,38.405231 72.874217,63.753189 Z" class="unclicked"/> <path style="opacity:1;fill:#008000;stroke-width:4.50001;stroke-linecap:round;stroke-miterlimit:7" id="hexagon2" d="M 87.810242,88.579156 72.874219,63.753189 86.906125,38.405231 115.87405,37.883239 130.81008,62.709205 116.77817,88.057164 Z" class="unclicked"/> </g> </svg>

Try to add !important to the style rules.尝试将!important添加到样式规则中。

This is because you also set fill in the style attribute of the paths which overwrites the fill rule from your classes这是因为您还设置了路径的样式属性,它会覆盖类中的填充规则

The issue is because you have used inline style attributes which override any external style rules.问题是因为您使用了覆盖任何外部样式规则的内联style属性。

To fix this you need to organise your CSS so that the rules of .clicked and .unclicked are specific enough to override any default styling.要解决此问题,您需要组织 CSS 以便.clicked.unclicked的规则足够具体,可以覆盖任何默认样式。 You also need to make the fill colour of both of those classes different, as in your example they are identical and will make no difference.您还需要使这两个类的fill颜色不同,因为在您的示例中它们是相同的并且不会有任何区别。 Try this:尝试这个:

 $('.hexagon').click(function() { $(this).toggleClass("unclicked clicked"); // note the single use of toggleClass here });
 .hexagon { opacity: 1; stroke-width: 4.50001; stroke-linecap: round; stroke-miterlimit: 7; }.hexagon1 { fill: #ffb619; }.hexagon2 { fill: #008000; } svg path.clicked { fill: #0000FF; } svg path.unclicked { fill: #CC0000; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <svg viewBox="0 0 101.83981 74.999893" id="svg8"> <g id="layer1" transform="translate(-28.970265,-13.579265)"> <path class="hexagon hexagon1 unclicked" d="M 43.906288,64.275181 28.970265,39.449215 43.002172,14.101257 71.9701,13.579265 86.906123,38.405231 72.874217,63.753189 Z" /> <path class="hexagon hexagon2 unclicked" d="M 87.810242,88.579156 72.874219,63.753189 86.906125,38.405231 115.87405,37.883239 130.81008,62.709205 116.77817,88.057164 Z" /> </g> </svg>

Two things.两件事情。

  1. you have inline-styles in both the cases, so all the properties in inline-style will not be applied as inline-styles have higher specifity.在这两种情况下您都有内联样式,因此内联样式中的所有属性都不会被应用,因为内联样式具有更高的特异性。

  2. Both unclicked and clicked have the same CSS未点击和clicked都具有相同的unclicked

     .clicked { fill: #0000FF; }.unclicked { fill: #0000FF; }

To make it work either use !important in your JQuery or avoid inline-style initially and apply styles in a CSS file.要使其正常工作,请在您的 JQuery 中使用!important important 或避免最初使用内联样式并在 CSS 文件中应用 styles 。 Plus you definitely need to use different fill color in both the cases.另外,在这两种情况下,您肯定需要使用不同的fill颜色。

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

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