admin管理员组文章数量:1356227
I'm new at ES6 and I'm trying to display to none a div when an inner button is clicked.
I tried this but it's not working:
const hide = () => {
const z = document.getElementById('button')
const y = document.getElementById('block')
z.onclick = () => {
y.style.display='none'
}
}
<div id="block">
<button id="button" onClick="hide()">my button</button>
</div>
I'm new at ES6 and I'm trying to display to none a div when an inner button is clicked.
I tried this but it's not working:
const hide = () => {
const z = document.getElementById('button')
const y = document.getElementById('block')
z.onclick = () => {
y.style.display='none'
}
}
<div id="block">
<button id="button" onClick="hide()">my button</button>
</div>
Any help please?
Share Improve this question edited Nov 14, 2017 at 11:43 3Dos 3,4973 gold badges26 silver badges39 bronze badges asked Nov 14, 2017 at 11:15 BeeLeeBeeLee 1935 silver badges15 bronze badges2 Answers
Reset to default 5You are attaching an event handler for first time. After the second time it will work and will again attach an event handler.
You don't need to use inline event handler, avoid this type of event handler attachement. You can just remove the hide
function part serving the content. When your code runs, it will attach the event handler
const z = document.getElementById('button');
const y = document.getElementById('block');
z.onclick = () => {
y.style.display = 'none';
};
<div id="block">
<button id="button">my button</button>
</div>
Instead of hiding the div
, your hide method is assigning a new onclick
handler to your button.
Simply
const hide = () => {
document.getElementById('block').style.display='none'
}
本文标签: javascriptES6 Display div to none when button is clickedStack Overflow
版权声明:本文标题:javascript - ES6 Display div to none when button is clicked - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744005153a2574564.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论