javascript - 如何在需要时突出显示替代隐藏复选框输入的复选框伪元素

标签 javascript html css

我有一个复选框伪元素,它替代了隐藏的复选框输入,我想在需要隐藏的复选框时突出显示它。有什么css 解决方案吗?解决问题的正确方法是什么? https://jsfiddle.net/ht2c9sbd/3/

<form>
  <div class="group-input">
    <input type="checkbox" id="c_1" required hidden="">
    <label for="c_1">I agree with everything.</label>
  </div>
  <input type="submit" value="submit" id="submit">
</form>

CSS:

input[type="checkbox"] + label::before {
    content: "";
    display: inline-block;
    height: 30px;
    width: 30px;
    margin: 0 14px 0 0;
    cursor: pointer;
    vertical-align: middle;
    position: absolute;
    left: 0;
    top: calc(50% - 15px);
    -webkit-transition: background-color 0.25s ease, border-color 0.25s ease;
    transition: background-color 0.25s ease, border-color 0.25s ease;
}

input[type="checkbox"] + label::before {
    content: "";
    display: inline-block;
    height: 30px;
    width: 30px;
    border: 1px solid rgba(20, 61, 139, 0.2);
    margin: 0 14px 0 0;
    cursor: pointer;
    vertical-align: middle;
    position: relative;
    top: -2px;
}

input[type="checkbox"]:checked + label::before {
    background-image: url(https://image.flaticon.com/icons/png/512/447/447147.png);
    background-size: 15px;
    border: 1px solid rgba(20, 61, 139, 0);
    background-color: rgba(20, 61, 139, 0.2);
    background-position: center;
    background-repeat: no-repeat;
}

这是我糟糕的 js 解决方案:

function checkCheckbox(e) {
    if (!e.checked) {
        $('head').append('<style id="invalidCheckbox">input[type="checkbox"] + label::before {border: 1px solid #ff3535; box-shadow: 0 0 2px #ff3535;}</style>');
        return false;
    } else {
        let temp = $('head style:last');
        while(temp && temp[0].id === "invalidCheckbox"){
            temp.remove();
            temp = $('head style:last');
        }
        return true;
    }
}

const mycheckbox = document.getElementById('c_1');
document.getElementById("submit").addEventListener('click', function(e){
  e.preventDefault();
  checkCheckbox(mycheckbox); 
});

最佳答案

脚本:

const mycheckbox = document.getElementById('c_1');
document.getElementById("submit").addEventListener('click', function(e){
    e.preventDefault();
    if ( mycheckbox.hasAttribute('required') && !mycheckbox.checked ){
    mycheckbox.className = "invalid-input"; // <-- add new class
  } else {
    mycheckbox.className = "";
  }
});

风格(你可以随意改变风格):

input[type="checkbox"].invalid-input + label::before{
  background-color: red;
}

Try to update the existing DOM element instead of creating a new element in DOM because DOM manipulation is slow.

A repaint occurs when changes are made to an elements skin that changes visibly but does not affect its layout.

Create new DOM element is even more critical to performance because it involves changes that affect the layout of a portion of the page

关于javascript - 如何在需要时突出显示替代隐藏复选框输入的复选框伪元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53953188/

相关文章:

html - Express.js 不发送 css 文件

javascript - 如何使用下一个 js 加速 getServerSideProps?

javascript - 如何从html元素获取数组索引?

javascript - 滚动到名称为 "foo"的随机类

javascript - 你如何重构 JavaScript、HTML、CSS 等?

css - png 文件不在 IE 中显示

html - Flex/Grid 布局不适用于按钮或字段集元素

javascript - 找到key然后替换value

html - 如何使图像按比例缩放以适应 Web 浏览器窗口的大小调整?

javascript - Simogeo FileManager 查看/上传