简体   繁体   English

更改样式的Onload功能不起作用

[英]Onload function for change style doesn't work

Well, i want that when body is completely loaded then call a function for show a hide image. 好吧,我希望当主体完全加载后,再调用一个函数来显示隐藏图像。 I tryed a lot of things and searched a lot but anything work. 我尝试了很多事情,进行了很多搜索,但是一切正常。 I think it's pretty simple and easy but i don't know why doesn't work...I wish that anyone be able to help me. 我认为这非常简单和容易,但是我不知道为什么不起作用...我希望任何人都能帮助我。

<body onload="start()">
<div id="bg" style="display:none"></div>
</body>

function start() {
document.getElementById("bg").style.display = 'block';
}

Here Jsfiddle link 这里的Jsfiddle链接

just remove onload="start()" from your body and replace your function start for 只需从您的身体中删除onload =“ start()”并替换您的函数start

<script>
    window.onload=function() {
        document.getElementById("bg").style.display = 'block';
    };
</script>

if you want use jquery, then 如果要使用jQuery,则

<script>
    $(document).ready(function (){
        $("#bg").css("display", "block");
    });
</script>

That's because you need to run your Javascript inside the body tag. 那是因为您需要在body标签内运行Javascript。 Actually at the end. 实际上在最后。 Conversely, the start function will be undefined and nothing happens: 相反, start函数将是未定义的,什么也不会发生:

<body onload="start()">
    <div id="bg" style="display:none"></div>
    <script>
        function start() {
            document.getElementById("bg").style.display = 'block';
        }
    </script>
</body>

you have to option: 您必须选择:

1- add script at before starting 1-在开始之前在添加脚本

<script type="text/javascript">
    $(document).ready(function () {
        //Add your code here to run after fully page load
        document.getElementById("bg").style.display = 'block';
    });
</script>

2- or add your script at end of <body> before ending </body> tag: 2-或在<body>末尾</body>标记之前添加脚本:

<script type="text/javascript">
     //Add your code here to run after fully page load
     document.getElementById("bg").style.display = 'block';
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM