javascript - 使用复选框使 div 变灰

标签 javascript jquery html css

我需要在选中复选框时使 div 变灰/禁用。

有人可以帮我举个关于 JSfiddle 的例子吗?

这是我的代码:http://jsfiddle.net/albertoc/vFrAB/5/

HTML:

<div id="wrap" style="width:500px">
    <div id="content">
        <p>This box will be disabled! <a href="#">Link</a>
        </p>
    </div>
    <div id="darkLayer" class="darkClass" style="display:none"></div>
    <div id="checkbox">
        <form>
            <input type="checkbox" id="myCheckBox" />
        </form>
    </div>
</div>

CSS:

.darkClass
{
    background-color: white;
    filter:alpha(opacity=50); /* IE */
    opacity: 0.5; /* Safari, Opera */
    -moz-opacity:0.50; /* FireFox */
    z-index: 20;
    height: 100%;
    width: 100%;
    background-repeat:no-repeat;
    background-position:center;
    position:absolute;
    top: 0px;
    left: 0px;
}

JS:

function dimOff()
{
    document.getElementById("darkLayer").style.display = "none";
}
function dimOn()
{
    document.getElementById("darkLayer").style.display = "";
}

最佳答案

我将您的两个功能合二为一,并为您的复选框提供了比叠加层更高的 z-index,以使其保持可点击状态。

function dim(cb) {
    document.getElementById("darkLayer").style.display = (cb.checked) ? '' : 'none';
}

jsFiddle example

CSS

#checkbox {
    position:relative;
    z-index:21;
}

HTML

<div id="wrap" style="width:500px">
    <div id="content">
        <p>This box will be disabled! <a href="#">Link</a></p>
    </div>
    <div id="darkLayer" class="darkClass" style="display:none"></div>
    <div id="checkbox">
        <form>
            <input type="checkbox" id="myCheckBox" onchange="dim(this)" />
        </form>
    </div>
</div>

关于javascript - 使用复选框使 div 变灰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18467216/

相关文章:

javascript - HTML > 根据窗口和图像大小旋转和缩放图像

javascript - 将 jquery(.each, .find,.next) 转换为 javascript

javascript - 如何使用jquery从div中的多个类中获取指定的类名?

jquery - 在 jQuery AJAX 请求中包含嵌套的 HttpPostedFileBase

html - CSS - 两列表格

php - 在另一个 div 中显示一个 div 的菜单链接

javascript - Promise 在 setTimeOut 之后没有得到解决

jquery - Bootstrap 数据表分页显示在新行中

javascript - 我如何使用 html5 在设备中获得摄像头和麦克风?

javascript - 是否有一个事件监听器用于在提交之前验证 html5 表单?