【问题标题】:Styling label css using jquery使用 jquery 样式化标签 css
【发布时间】:2014-08-25 17:49:13
【问题描述】:

我有一个广播组,它动态地进入我的页面。我能够使用 php 成功禁用任何收音机,但我想在禁用的收音机标签上添加 text-decoration: 的 css 属性。这是代码:

<div class="form-item form-type-radio form-item-example-pane-time-slot-1">
    <input type="radio" id="edit-example-pane-time-slot-1-11" name="example_pane[time_slot_1]" value="11" class="form-radio">
    <label class="option" for="edit-example-pane-time-slot-1-11">11 - 12 pm </label>
</div>

如何使用 jquery ?我的 jquery 代码是这样的:

if ($('#edit-example-pane-time-slot-1-11').attr('disabled',true))
{
    $(this).css('text-decoration', 'line-through');
}

它不工作。

【问题讨论】:

    标签: javascript php jquery css drupal-7


    【解决方案1】:

    你应该检查一下

    if ($('#edit-example-pane-time-slot-1-11').attr('disabled')===true))
        {$(this).siblings('label').css('text-decoration', 'line-through');}
    

    或者只是禁用它,你应该使用.attr('disabled','disabled')。看来你把这两个混在一起了。

    PS:请阅读 JavaScript 中 == 和 === 之间的区别。

    【讨论】:

    • 我知道区别 === 是不是一样的?
    • 有区别。此外,您输入的标签似乎没有正确制定。
    • 其实它已经被禁用了,我只是想在检查收音机是否禁用后将css属性添加到标签。
    【解决方案2】:

    你可以用 css 做到这一点

    CSS adjacent sibling selector

    CSS :disabled pseudo-class

    .form-radio:disabled + .option {
        text-decoration: line-through;
    }
    

    FIDDLE

    【讨论】:

    • 好的。但是我将如何定位该组中的 1 个收音机,因为我已经提到它是一个收音机组而不是单个单选按钮
    • 谢谢ᾠῗᵲᄐᶌ 这是解决我的小问题的最佳方法。感谢您的回答。
    【解决方案3】:

    这是为其添加 css 的代码。

    $(this).siblings('label').css('text-decoration','line-through');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-01
      • 2013-07-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多