javascript - element.onclick 在点击时没有反应
问题描述
单击某个图标时,我试图显示/隐藏选项,但它似乎不起作用。我已经验证了链接,并通过警报对其进行了测试。这是我的 HTML:
<!DOCTYPE html>
主页
<main>
<table class='menu'>
<thead>
<tr scope='col'>
<th id='menu-icon'>
<img src='./images/menu.png' ☰>
</th>
<th rowspan="2" id='hide-insta'>
#LoveLan<img id="insta" src='./images/instagram.png'>
<p>en <img src="arrow-down.png"></p>
</th>
<th id='show-profile'>
<div><img src='./images/Profile.png'>My profile</div>
<div><img src="images/info-circle.png">Info & Faqs</div>
</th>
</tr>
</thead>
<tbody class='white-squares'>
<tr>
<td id='yellow'>WHAT'S ON IN LONDON</td>
<td id='blue'>YOUR LONDON CALENDAR</td>
</tr>
<tr>
<td id='blue'>SERVICES</td>
<td id='yellow'>OFFERS</td>
</tr>
</tbody>
</table>
</main>
<script type="text/javascript" src="main.js"></script>
</body>
在 CSS 上我有 - 这是否会影响 js 文件并使显示始终为“show-profile”隐藏?:
#show-profile {
color: #d0a955;
font-size: 23px;
font-family: 'Gill Sans';
display: none;
}
最后,我的js文件:
const toHide = document.getElementById('hide-insta');
const menuIcon = document.getElementById('menu-icon');
const profileAndFaqs = document.getElementById('show-profile');
const displaySetting = toHide.style.display;
let displayProfile = () => {
toHide.style.display = 'none';
profileAndFaqs.style.display = 'inline-block';
}
let reset = () => {
toHide.style.display = 'inline';
profileAndFaqs.style.display = 'none';
}
let toggleInsta = () => {
if(displaySetting === none) {
reset()
} else {
displayProfile()
}
}
menuIcon.onclick(toggleInsta);
解决方案
推荐阅读
- azure - Terraform - 创建 Azure 事件网格订阅 - 可以吗?
- c++ - 编译器在尝试使用继承定义类时出错
- laravel - 如何在 Redis 队列中调度延迟(待处理)作业?
- javascript - Azure DevOps 在部署之前更新代码内的变量
- python - Pygame中如何正确继承Surface类
- php - Laravel 中中间件的对立面是什么?
- tensorflow - 为什么 ImageDataGenerator() 表现不佳?
- python - 在与列表Python中范围内的数字不对应的位置删除字符串中的字母
- php - 如何更正我的代码以在 Excel 文档的不同工作表上打印
- scala - sbt 卡住并进入 OutOfMemory