【问题标题】:Two span elements inside label标签内的两个跨度元素
【发布时间】:2021-02-08 00:05:43
【问题描述】:

在标签内我想有两个 span 元素。请参阅 jsfiddle 的示例:https://jsfiddle.net/c7bz1gf2/

 <div class = 'div_test'>
  <div class="checkbox">
     <input type="checkbox" id="id_chxbox" name="" value="">
     <label for="id_chxbox"><span>Checkbox 1  </span> <span class='detail_view'>  [detail] </span></label>
  </div>

 </div>

(在这个问题中找到了复选框的样式:How to change the background color on a input checkbox with css?

我只想在单击“详细信息”范围时“提醒”..当此事件触发时,它不应将复选框标记为“已选中”..

谁能帮我解决这个问题?

【问题讨论】:

    标签: javascript html css


    【解决方案1】:

    尝试使用&lt;div class='detail_view' style="display: inline;"&gt;...&lt;/div&gt;,然后您可能需要修复边距和填充

    更新:

    $('.detail_view').on('click', function (e) { 
        e.preventDefault(); // this prevents the check
        alert("text");
    });
    .checkbox input[type="checkbox"] {
        width: auto;
        opacity: 0.00000001;
        position: absolute;
        left: 0;
        margin-left: -20px;
        
    }
    .checkbox label {
        position: relative;
    }
    .checkbox label:before {
        content: '';
        position: absolute;
        left: 0;
        top: 0;
        margin: 4px;
        width: 20px;
        height: 20px;
        transition: transform 0.28s ease;
        border-radius: 3px;
        border: 1px solid ;
    }
    .checkbox label:after {
      content: '';
        display: block;
        width: 9px;
        height: 4px;
        border-bottom: 2px solid #7bbe72;
        border-left: 2px solid #7bbe72;
        -webkit-transform: rotate(-45deg) scale(0);
        transform: rotate(-45deg) scale(0);
        transition: transform ease 0.25s;
        will-change: transform;
        position: absolute;
        top: 10px;
        left: 9px;
    }
    .checkbox input[type="checkbox"]:checked ~ label::before {
        color: #7bbe72;
    }
    
    .checkbox input[type="checkbox"]:checked ~ label::after {
        -webkit-transform: rotate(-45deg) scale(1);
        transform: rotate(-45deg) scale(1);
    }
    
    .checkbox label {
        min-height: 30px;
        display: block;
        padding-left: 30px;
        margin-bottom: 0;
        font-weight: normal;
        cursor: pointer;
        vertical-align: sub;
        font-size: 16px;
    }
    .checkbox label span {
        position: absolute;
        top: 50%;
        -webkit-transform: translateY(-50%);
        transform: translateY(-50%);
    }
    
    .detail_view {
        margin-left: 100px;
    }
    <!-- Jquery Inclusion -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    <!-- actual code -->
    <div class = 'div_test'>
      <div class="checkbox">
         <input type="checkbox" id="id_chxbox" name="" value="">
         <label for="id_chxbox">
           <div style="display: inline-block;">Very Long Checkbox Text ................. 1</div>
           <div class='detail_view' style="display: inline-block;">[details]</div>
         </label>
      </div>
     </div>

    【讨论】:

    • 我试过了,它几乎可以工作,..但复选框仍然标记为“已选中”.. :/
    • @zoki182 对不起,我忘记了 JS 的变化
    • 谢谢!您能否帮我确定这个“新 div”元素的位置.. 我希望它始终定位,让我们说 span 元素左侧 10px,无论文本(例如 Checkbox1 或 Checkbooxxxxxx123)如何长..
    • @zoki182 我已经编辑了插入一个 sn-p,只是一个注释:将 .detail_view 的填充转换为 margin-left: 10px 或者您可以单击左侧填充的空白并触发回调(pad属于元素内容,margin不属于)
    【解决方案2】:

    将 detail_view 跨度放置在标签元素之外应该可以解决问题

    【讨论】:

    • 我想在同一行有“detail_view”..
    【解决方案3】:

    您需要停止向上冒泡事件。

    $('.div_test').on('click', 'span.detail_view', function (e) {
       e.stopPropagation()                    
       alert("text");
    }
    

    【讨论】:

    • 嘿,谢谢您的回复,但这不是我想要的方式..它仍然将复选框标记为选中...但是无论如何,@DDomen 为我提供了一个解决方案..还是谢谢你:)