gpt4 book ai didi

Javascript 用复选框改变颜色

转载 作者:行者123 更新时间:2023-11-27 23:37:32 25 4
gpt4 key购买 nike

我这里有一个 JavaScript,它可以在选中复选框时改变颜色,但它必须依赖于外部库的使用才能工作。是否可以不使用诸如function()之类的外部库?

<p style="color: red; font-weight: bold;">I have read and agree to the terms and conditions
<input type="checkbox" id="termsChkbx" onchange="isChecked(this,'sub1')"/></p>

JS:

<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
$(document).ready(function(){
$('#termsChkbx').change(function(){
if($(this).is(':checked'))
{
$(this).parent('p').css('color','black');
}
else
{
$(this).parent('p').css('color','red');
}
});

最佳答案

您可以使用:

function isChecked(elem) {
elem.parentNode.style.color = (elem.checked) ? 'black' : 'red';
}

<强> jsFiddle example

关于Javascript 用复选框改变颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34002051/

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