onclick calling hide-div function not working

雨燕双飞 提交于 2020-01-19 04:10:35

问题


I've been fighting with this simple piece of code forever. I'm trying to use a <button> to call a simple JavaScript function to hide a <div>.

HTML:

<button type="button" onclick="close('wrong')">OK</button>

JavaScript:

function close(ID)
{
    document.getElementById(ID).style.display="none";
}

I'm sure it's something really simple, I just can't, for the life of me, find it.


回答1:


Try changing the close function name to something else like closeit because it may be confusing it with the window.close() function.




回答2:


Try document.getElementById('wrong').setAttribute('style', 'display:none');




回答3:


Let's do something in jQuery:

<button type="button" id="btn">OK</button>
<div id="myDiv">Div's contents here</div>

Now, you need just use the code below to do what you want:

<script type="javascript">
$(document).ready(function(e) {    
   $("#btn").click(function () {
      $("#myDiv").hide();
   });
});
</script>

Notice: you can have some properties for hide() function: Fast and slow. This will affect on the speed of hiding action.

$("#myDiv").hide("slow");

I hope this can help you.



来源:https://stackoverflow.com/questions/8550828/onclick-calling-hide-div-function-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!