【发布时间】:2019-09-16 00:35:30
【问题描述】:
我有一个基于按钮点击隐藏和显示的 div。这非常有效,但我希望能够根据是否选中复选框来执行此操作。
<html>
<head>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
</head>
<body>
<div id="box" class="box">
<table border="1" cellpadding="1" cellspacing="1" style="width: 500px;">
<tbody>
<tr>
<td>test text</td>
<td>1</td>
</tr>
<tr>
<td>in dummy</td>
<td>2</td>
</tr>
<tr>
<td>table</td>
<td>3</td>
</tr>
</tbody>
</table>
</div>
<button>TOGGLE VISIBILITY</button>
<input type="checkbox" id="2" name="display" value="Y">
<script>
try {
var box = $('#box');
$('button').on('click', function() {
if (box.hasClass('hidden')) {
box.removeClass('hidden');
setTimeout(function() {
box.removeClass('visuallyhidden');
}, 20);
} else {
box.addClass('visuallyhidden');
box.one('transitionend', function(e) {
box.addClass('hidden');
});
}
});
} catch (error) {
throw error;
}
</script>
</body>
</html>
此示例查看按钮,但我希望它引用复选框的状态
【问题讨论】:
-
您的按钮在上面给出的示例中似乎没有做任何事情。我想你忘了添加相关的 CSS 类。
标签: javascript