【问题标题】:Style label when input inside of it is checked检查其中输入时的样式标签
【发布时间】:2020-04-11 23:07:18
【问题描述】:

我不知道如何使这项工作。我在下面有 HTML 代码。我想在标签上应用样式,当检查输入时,我想设置标签背景的样式。有什么解决方案吗,因为我无法使用 input:checked+label, label+input:checked.... 没有任何效果。

<ul class="wcsatt-options-prompt-radios">
<li class="wcsatt-options-prompt-radio">
    <label type="radio" class="wcsatt-options-prompt-label wcsatt-options-prompt-label-one-time">
        <input class="wcsatt-options-prompt-action-input" type="radio" name="subscribe-to-action-input" value="no" />
        <span class="wcsatt-options-prompt-action"><?php echo $one_time_cta; ?></span> 
        <span class="subs-box-price">Price</span>
        <div class="sub-sub-text">Save 20$</div>        
    </label>
</li>

提前致谢

【问题讨论】:

    标签: html css input label checked


    【解决方案1】:

    更新:

    我使用 JQuery 添加了这个替代解决方案,因为您的标签是输入的父级,目前无法使用 CSS Combinators 选择子级的父级:

    $('#radio-label').click(function() {
       if($('#input-radio').is(':checked')) { 
       $('#radio-label').toggleClass('has-background-color'); }
    });
    .has-background-color {
      background-color: cyan;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    
    <ul class="wcsatt-options-prompt-radios">
    <li class="wcsatt-options-prompt-radio">
        <label id="radio-label" type="radio" for="input-radio" class="wcsatt-options-prompt-label wcsatt-options-prompt-label-one-time">
            <input id="input-radio" class="wcsatt-options-prompt-action-input" type="radio" name="subscribe-to-action-input" value="no" />
            <span class="wcsatt-options-prompt-action"><?php echo $one_time_cta; ?></span> 
            <span id="subs-box-price-text" class="subs-box-price">Price</span>
            <div class="sub-sub-text">Save 20$</div>        
        </label>
    </li>

    原帖:

    您应该使用“~”组合符,而不是使用“+”。

    使用“for”属性(在 label 元素中)和“id”属性(在 input 元素中)将您的标签连接到您的输入。

    然后,在您的 CSS 选择器中使用波浪符“~”,如下所示,这被称为“后续兄弟组合器”,因为您的 input 元素和您的 span 元素共享同一个父元素。

    引用自:https://www.geeksforgeeks.org/what-does-symbol-tilde-denotes-in-css/

    您使用“+”这一事实不起作用,因为它用于选择下一个直接同级元素。

    您可以查看 CSS Combinators 以了解更多详细信息: https://www.w3schools.com/css/css_combinators.asp

    在代码中,我只更改了价格范围标签的背景颜色。您可以根据需要修改代码。

    #input-radio:checked~#subs-box-price-text {
      background-color: cyan;
    }
    <ul class="wcsatt-options-prompt-radios">
    <li class="wcsatt-options-prompt-radio">
        <label type="radio" for="input-radio" class="wcsatt-options-prompt-label wcsatt-options-prompt-label-one-time">
            <input id="input-radio" class="wcsatt-options-prompt-action-input" type="radio" name="subscribe-to-action-input" value="no" />
            <span class="wcsatt-options-prompt-action"><?php echo $one_time_cta; ?></span> 
            <span id="subs-box-price-text" class="subs-box-price">Price</span>
            <div class="sub-sub-text">Save 20$</div>        
        </label>
    </li>

    【讨论】:

    • 嗯,但我想设置标签元素的背景样式。尝试了您的解决方案,但不适用于标签?
    • 我添加了一个使用 JQuery 的替代解决方案。我同意@Bill Criswell 的观点,即您不能根据孩子的状态来设置父母的样式。
    • 太棒了 hiew1,非常感谢你,就像一个魅力。
    【解决方案2】:

    您不能根据孩子的状态设置样式。使用您正在使用的选择器(兄弟选择器),您似乎希望标签成为单选按钮的兄弟。

    <input type="radio">
    <label>Hi</label>
    

    那你就可以:checked + label { background-color: red }了。

    在 cmets 中,您询问了如何保留标记并使用 jQuery 执行此操作。您可以执行以下操作:

    $('input[type="radio"]').on('change', function(event) {
      const $input = $(this)
      $input.closest('label').toggleClass('checked', $input.checked)
    })
    

    【讨论】:

    • 我需要将输入保留在标签内。那么有没有办法风格呢?或者用 jQuery 有可能吗?
    • 添加了快速编辑。比纳。虽然自从我接触了 jQuery 但应该可以工作。
    猜你喜欢
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 2018-01-02
    • 2015-03-12
    • 2016-10-06
    • 2015-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多