如何解决使用Jquery隐藏和显示div
| 我试图隐藏一个div,这很容易在jquery中使用hide函数完成。但是它不起作用。div是<div id=\"antivirusAlertDiv\">
<ul class=\"shielding_elements\">
<li>
<%= Html.RadioButton(\"antivirusAlertType\",antivirusAlertType == AntivirusAlertType.AlertMe ? true : false)%>
<label for=\"id_4\">
Alert Me</label>
</li>
<li>
<%= Html.RadioButton(\"antivirusAlertType\",antivirusAlertType == AntivirusAlertType.DonotAlert ? true : false)%>
<label for=\"id_5\">
Don’t Alert</label>
</li>
</ul>
</div>
jQuery代码是:
function ShowAndHide(id,state) {
var $mydiv = $(\'#antivirusAlertDiv\');
if (state == false) {
$mydiv.hide().html(\'\');
// $(\'#uniform-antivirusAlertType\').hide();
// $(\'#antivirusAlertDiv .shielding_elements li\').hide();
// $(\"input[name=\'antivirusAlertType\']\").attr(\"disabled\",\"disabled\");
// $(\"#\" + id).addClass(\"hide\");
// $(\"#\" + id).removeClass(\"show\");
}
else {
$mydiv.show();
//$(\'#uniform-antivirusAlertType\').show();
// $(\'#antivirusAlertDiv .shielding_elements li\').show();
//$(\"input[name=\'antivirusAlertType\']\").removeAttr(\"disabled\");
// $(\"#\" + id).removeClass(\"hide\");
// $(\"#\" + id).addClass(\"show\");
}
}
解决方法
使用
.toggle()
。但是,您实际上需要将其绑定到某个事件。例如,如果HTML如下所示:
<button id=\"clickme\">Click to toggle</button>
<div id=\"antivirusAlertDiv\">
<!-- snip -->
</div>
然后在准备好文档的处理程序中,
$(\'#clickme\').click(function ()
{
$(\'#antivirusAlertDiv\').toggle();
});
演示:http://jsfiddle.net/mattball/rPYLK/
N.B.您可以简化现有代码,如下所示:
<div id=\"antivirusAlertDiv\">
<ul class=\"shielding_elements\">
<li>
<label>
<%= Html.RadioButton(\"antivirusAlertType\",antivirusAlertType == AntivirusAlertType.AlertMe)%>
Alert Me
</label>
</li>
<li>
<label>
<%= Html.RadioButton(\"antivirusAlertType\",antivirusAlertType == AntivirusAlertType.DonotAlert)%>
Don’t Alert
</label>
</li>
</ul>
</div>
条件运算符是多余的
将ѭ6包裹在相应的输入周围,并可以省略for
属性
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。