gpt4 book ai didi

javascript - 用复选框替换切换按钮但不起作用

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

我用复选框替换了 jQuery 切换代码中的按钮,但无法使其正常工作。

这是我尝试用复选框替换按钮的 HTML 文件

index.html

<p> hi </p>
<!--<button>Press me</button>-->
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked>
<label class="onoffswitch-label" for="myonoffswitch"></label>
</div>

javascript

$(function() {

if (Cookies) {
$("p").toggle(!( !! Cookies("toggle-state")) || Cookies("toggle-state") === 'true');
}

// $('button').on('click', function()
$('#myonoffswitch.onoffswitch').is(":checked"), function()
{
$("p").toggle();
Cookies.set("toggle-state", $("p").is(':visible'), {
expires: 1,
path: '/'
});
});

});

Fiddle

这里有什么问题吗?

最佳答案

$(function() {


$('#myonoffswitch').click(function(){
/*Use toggle if you need only hide and show */
//$("p").toggle();

/*If you want to know the current status of checkbox go with below code*/
if($(this).is(':checked')){
$("p").show();
}else{
$("p").hide();
}
})


});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p> hi </p>
<!--<button>Press me</button>-->
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked>
<label class="onoffswitch-label" for="myonoffswitch"></label>
</div>

  $('#myonoffswitch.onoffswitch').is(":checked"), function() 
{
$("p").toggle();
Cookies.set("toggle-state", $("p").is(':visible'), {
expires: 1,
path: '/'
});
});

将此代码更改为

       $('#myonoffswitch').click(function(){
/*Use toggle if you need only hide and show */
//$("p").toggle();

/*If you want to know the current status of checkbox go with below code*/
if($(this).is(':checked')){
$("p").show();
}else{
$("p").hide();
}
})

关于javascript - 用复选框替换切换按钮但不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43892789/

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