CSS 对动态类没有影响

发布于 2024-08-16 03:08:47 字数 692 浏览 5 评论 0原文

$('fieldset.one label, fieldset.two label').each(function () {
    var className = $(this).text().trim().toLowerCase();
    $(this).addClass('default ' + className);
});

我正在向标签元素动态添加类,但是当我尝试设置它们的样式时,没有任何效果。其中之一自动添加了 .sedan 类,我在 firebug 中看到它,但以下内容没有任何效果:

.sedan {
  display:none !important;
}

HTML:

<fieldset class="one">
  <label>sedan</label>
  <label>suv</label>
  <label>truck</label>
</fieldset>

<fieldset class="two">
  <label>sedan</label>
  <label>suv</label>
  <label>truck</label>
</fieldset>
$('fieldset.one label, fieldset.two label').each(function () {
    var className = $(this).text().trim().toLowerCase();
    $(this).addClass('default ' + className);
});

I'm adding classes dynamically to label elements, but when I try to style them, there is no effect. One of them has .sedan class added automatically, I see it in firebug, but the following doesn't have any effect:

.sedan {
  display:none !important;
}

HTML:

<fieldset class="one">
  <label>sedan</label>
  <label>suv</label>
  <label>truck</label>
</fieldset>

<fieldset class="two">
  <label>sedan</label>
  <label>suv</label>
  <label>truck</label>
</fieldset>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

反差帅 2024-08-23 03:08:47

使用 jQuery 的 trim 方法:

var className = $.trim($(this).text()).toLowerCase();

Use jQuery's trim method:

var className = $.trim($(this).text()).toLowerCase();
许久 2024-08-23 03:08:47

您在这里发布的代码没有任何问题。我刚刚在本地测试了一下,效果很好。确保您的选择器正确 - 并且您可以检查样式是否是通过 firebug 添加的。

There's nothing wrong with the code you've posted here. I just tested it locally and it works just fine. Make sure your selectors are correct - and you can check to see whether the styles are being added with firebug.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文