gpt4 book ai didi

jquery如何给父对象添加属性?

转载 作者:太空宇宙 更新时间:2023-11-04 13:07:36 24 4
gpt4 key购买 nike

情况是这样

我有这个 jquery 代码

$("li").click(function(){
var checkbox = $(this).find("input[type='checkbox']");

if( checkbox.prop("checked") == "" ){
checkbox.prop("checked", true);
(this).addClass("on");
} else {
checkbox.prop("checked", false);
("li").removeClass("on");
}
});

还有这个 html

<ul id="id_priori">
<li><label for="id_priori_0"><input id="id_priori_0" name="priori" type="checkbox"> Hola</label></li>
</ul>

我想做的是添加/删除

  • 上的类,具体取决于复选框是选中还是未选中,但它不起作用

  • 最佳答案

    几个错误:

    变化:

    ("li").removeClass("on");

    收件人:

    $("li").removeClass("on"); // You are missing the $

    变化:

    (this).addClass("on");

    收件人:

    $(this).addClass("on"); // You are missing the $

    此外,使用 .is(:checked') 查看复选框是否被选中。

    试试这个:

    $(document).ready(function () {
    $('li').addClass('on');
    $("li").click(function () {
    var checkbox = $(this).find("input[type='checkbox']");
    if (checkbox.is(':checked')) {
    $(this).addClass("on");
    } else {
    $(this).removeClass("on");
    }
    });
    });

    JSFiddle Demo

    关于jquery如何给父对象添加属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24976415/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com