js实现列表框选项改变时隐藏或显示表格
时间: 2024-01-20 10:04:36 浏览: 28
你可以使用JavaScript来实现这个功能。首先,你需要获取到列表框和表格元素的引用。然后,你可以给列表框添加一个事件监听器,当选项改变时,触发一个函数来隐藏或显示表格。
以下是一个基本的示例代码:
HTML代码:
```html
<select id="select-box">
<option value="show">显示表格</option>
<option value="hide">隐藏表格</option>
</select>
<table id="table">
<thead>
<tr>
<th>表头 1</th>
<th>表头 2</th>
<th>表头 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>内容 1</td>
<td>内容 2</td>
<td>内容 3</td>
</tr>
</tbody>
</table>
```
JavaScript代码:
```javascript
const selectBox = document.getElementById('select-box');
const table = document.getElementById('table');
selectBox.addEventListener('change', function() {
if (selectBox.value === 'show') {
table.style.display = 'table';
} else if (selectBox.value === 'hide') {
table.style.display = 'none';
}
});
```
这个代码片段中,我们首先获取了`select`元素和`table`元素的引用,然后给`select`元素添加了一个`change`事件监听器。当`select`元素的值改变时,我们检查当前选项的值,如果是`show`,则将`table`元素的`display`属性设置为`table`,显示表格;如果是`hide`,则将`table`元素的`display`属性设置为`none`,隐藏表格。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)