当所有项目都标记为绿色时,如何计算变为绿色并显示<p>的项目?

时间:2019-05-13 09:39:23

标签: javascript jquery html css

我做了一个清单。现在,我想在所有复选框均为绿色时显示文本。我不知道如何计算该复选框。有人可以帮我这个代码吗?

$(document).ready(function() {
  $("i").click(function() {
    $(this).css("color", "#5c8c44");
    $(this).parent().css("font-weight", "bold");
  });

  $("#reset").click(function() {
    $("i").css("color", "red");
    $("li").css("font-weight", "normal");
  });
});
ul li {
  list-style: none;
}

ul li i {
  color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css">

<h2>Checklist</h2>
<ul>
  <li><i class="fas fa-check-square"></i> Item 1</li>
  <li><i class="fas fa-check-square"></i> Item 2</li>
  <li><i class="fas fa-check-square"></i> Item 3</li>
  <li><i class="fas fa-check-square"></i> Item 4</li>
  <li><i class="fas fa-check-square"></i> Item 5</li>
  <li><i class="fas fa-check-square"></i> Item 6</li>
  <li><i class="fas fa-check-square"></i> Item 7</li>
  <li><i class="fas fa-check-square"></i> Item 8</li>
  <li><i class="fas fa-check-square"></i> Item 9</li>
  <li><i class="fas fa-check-square"></i> Item 10</li>
</ul>
<button id="reset">Reset checklist</button>

<script src="https://code.jquery.com/jquery-3.4.1.js"></script>

3 个答案:

答案 0 :(得分:0)

如何保留绿色按钮的计数器?为此,您需要声明一个变量,例如var count = 0;,然后每当按钮变为绿色时,便会增加计数器,每单击一次“重置”,都将重置计数。然后,如果计数器达到某个计数,请执行您想做的事情:

  var count = 0;

  $("i").click(function() {
    $(this).css("color", "#5c8c44");
    $(this).parent().css("font-weight", "bold");

    count += 1;
    if(count > 5) { /*...*/ }
  });

  //...

答案 1 :(得分:0)

在这里尝试:https://jsfiddle.net/3c0uskLy/ https://jsfiddle.net/83jcvaLr/

$(document).ready(function() {
  $("i").click(function() {
    $(this).css("color", "#5c8c44").addClass("grencolor")
    $(this).parent().css("font-weight", "bold");
    ChkList = $(".grencolor").length;
    $("#count1").text("").append("you've marked (" + ChkList + ")/10 boxs");
    if ($("li").length == ChkList) {
      $("#count").text("").append("you've got everything!")
    } else {
      $("#count").text("").append(" you don't have all the items")
    }
  });

  $("#reset").click(function() {
    $("#count").text("").append("0")
    $("i").removeClass("grencolor")
    $("i").css("color", "red");
    $("li").css("font-weight", "normal");
  });
});
ul li {
  list-style: none;
}

ul li i {
  color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css">

<h2>Checklist</h2>
<p id=count1></p>
<ul>
  <li><i class="fas fa-check-square"></i> Item 1</li>
  <li><i class="fas fa-check-square"></i> Item 2</li>
  <li><i class="fas fa-check-square"></i> Item 3</li>
  <li><i class="fas fa-check-square"></i> Item 4</li>
  <li><i class="fas fa-check-square"></i> Item 5</li>
  <li><i class="fas fa-check-square"></i> Item 6</li>
  <li><i class="fas fa-check-square"></i> Item 7</li>
  <li><i class="fas fa-check-square"></i> Item 8</li>
  <li><i class="fas fa-check-square"></i> Item 9</li>
  <li><i class="fas fa-check-square"></i> Item 10</li>
</ul>
<p id=count></p>
<button id="reset">Reset checklist</button>

<script src="https://code.jquery.com/jquery-3.4.1.js"></script>

答案 2 :(得分:0)

您可以计算ul的子元素。我已修改您的代码以获得所需的结果。您可以check here。我创建了一个用于检查绿色等级的函数

$(document).ready(function() {
  $("i").click(function() {
    //$(this).css("color", "#5c8c44");
    $(this).addClass('green')
    $(this).parent().css("font-weight", "bold");
    check();
  });

  $("#reset").click(function() {
    $("i").css("color", "red");
    $("li").css("font-weight", "normal");
    document.getElementById('output').innerHTML = '';
  });

  function check() {
    let ul_li_count = document.getElementById('ul_class').childElementCount;
    var numItems = $('.green').length;
    //console.log(numItems);
    if (numItems == ul_li_count) {
      let output = `<span> All the checkboxs are Clicked <\/span>`;
      document.getElementById('output').innerHTML = output;
    }
  }


});
ul li {
  list-style: none;
}

ul li i {
  color: red;
}

.green {
  color: #5c8c44
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" rel="stylesheet">

<h2>Checklist</h2>
<ul id="ul_class">
  <li><i class="fas fa-check-square"></i> Item 1</li>
  <li><i class="fas fa-check-square"></i> Item 2</li>
  <li><i class="fas fa-check-square"></i> Item 3</li>
  <li><i class="fas fa-check-square"></i> Item 4</li>
  <li><i class="fas fa-check-square"></i> Item 5</li>
  <li><i class="fas fa-check-square"></i> Item 6</li>
  <li><i class="fas fa-check-square"></i> Item 7</li>
  <li><i class="fas fa-check-square"></i> Item 8</li>
  <li><i class="fas fa-check-square"></i> Item 9</li>
  <li><i class="fas fa-check-square"></i> Item 10</li>
</ul>
<div id="output"></div><button id="reset">Reset checklist</button>