javascript - 单击复选框时隐藏元素

标签 javascript jquery html css

我试图在单击复选框时隐藏元素。

不知何故它没有工作-

html

<input type="checkbox"  onchange="showAtidit()">
<p id="hidethis">
hide me
</p>

js

function showAtidit($)
{
    if ($(this).is(':checked')) 
    {
        alert("not hide");
        document.getElementById( "hidethis" ).style.display = 'block';
    }
    else
    {
        alert("hide");
        document.getElementById( "hidethis" ).style.display = 'none';
    }
};

http://jsfiddle.net/wv97mpcp/108/

怎么了?

谢谢!

最佳答案

因为您使用的是 jquery,只需使用 toggle() :

function showAtidit()
{
   $('#hidethis').toggle();
}

希望这对您有所帮助。

function showAtidit()
{
  $('#hidethis').toggle();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox"  onchange="showAtidit()">
<p id="hidethis">
hide me
</p>

关于javascript - 单击复选框时隐藏元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40397823/

相关文章:

javascript - jQuery 包装全部 : preserving hierarchy of DOM elements

javascript - 满足条件时从数组中提取值

jquery - 如何显示ajax加载图像

html - 如何在html和css中的侧边菜单中创建子菜单

JQuery点击方法不起作用

javascript - Pug (Jade) 动态添加内联属性

javascript - IE8 及更低版本的 RequireJS shim 问题

javascript - 在 HTML + Javascript web 中启动 couchdb 数据库

javascript - 如何遍历 htmlCollection

jquery - 使用固定标题偏移 anchor 链接