javascript - How to hide a div using jQuery -
In my index.html file there is a div with id = "menu" and I want to hide it using jQuery . My script. I wrote in js:
window.onload = function () {prepareEventHandlers (); } Create the Event Event Handlers () {var hideDiv = $ ("# menu"); Var crtButton = $ ("#have"); CrtButton.onclick = function () {hideDiv.hide (); }; }
But when I press the button on the webpage, the Divis is still there, only one way that works index.html I write:
& lt; Button type = "button" onclick = "$ ('# menu') Hide ()" id = "hide" & gt; HideDiv & lt; / Button & gt;
But since I have this bad style, why can not I use this by using my script.js code, do not want to use it?
After
Go with Lucas's answer or modify your own code
< Code> crtButton is a jQuery object so it does not contain the onclick
property, instead of the onclick property of the DOM element (which is the crtbutton [0]
)
Or just leave jQuery and use getElementById
window.onload = function () {prepareEventHandlers (); } Create the Event Event Handlers () {var hideDiv = document.getElementById ("Menu"); Var crtButton = document.getElementById ("hide"); CrtButton.onclick = function () {hideDiv.style.display = 'none'}; }
Comments
Post a Comment