简体   繁体   English

如何使用JQuery选择复选框和字体很棒的元素

[英]How to select checkbox and font awesome elements using JQuery

JQuery makes it easy to select an element by ID or classname but this is a bit beyond my knowledge. JQuery使通过ID或类名选择元素变得容易,但这超出了我的了解。

I have the following styles I want applied when a function is called. 调用函数时,我想应用以下样式。

.classname input[type="checkbox"] ~ i.fa.fa-circle-o {
     color: grey;
}


.classname input[type="checkbox"]:checked ~ i.fa.fa-check-circle-o {
     color: grey;
}

So I tried putting them inside a function like this: 所以我尝试将它们放在这样的函数中:

function greyOut(classname) {
    console.log("fired");

    $("." + classname + " input[type='checkbox'] ~ i.fa.fa-circle-o").css("color: grey");
    $("." + classname + " input[type='checkbox']:checked ~ i.fa.fa-check-circle-o").css("color: grey");
}

But this is not working at all. 但这根本不起作用。 The function fired but no change in color for those elements and no error in console. 该功能已触发,但这些元素的颜色没有更改,并且控制台没有错误。

Could you simplify your code by using addClass() instead? 您可以使用addClass()来简化代码吗?

Example: 例:

 function greyOut(classname) { $(classname).addClass("checked"); } 
 .classname.checked input[type="checkbox"]~i.fa.fa-circle-o, .classname.checked input[type="checkbox"]:checked~i.fa.fa-check-circle-o { color: grey; } 
 <div class="classname"> <input type="checkbox"> </div> 

IMO it's just bad jQuery syntax. IMO,这只是糟糕的jQuery语法。 When you are changin in .css() method just oen property, you must use another syntax. .css()方法中更改属性时,必须使用其他语法。 So, you have these two possibilities: 因此,您有以下两种可能性:

function greyOut(classname) {
  console.log("fired");
  $("." + classname + " input[type='checkbox'] ~ i.fa.fa-circle-o").css("color", "grey");
  $("." + classname + " input[type='checkbox']:checked ~ i.fa.fa-check-circle-o").css("color", "grey");
}

Or: 要么:

function greyOut(classname) {
  console.log("fired");
  $("." + classname + " input[type='checkbox'] ~ i.fa.fa-circle-o").css({color: 'grey'});
  $("." + classname + " input[type='checkbox']:checked ~ i.fa.fa-check-circle-o").css({color: 'grey'});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM