简体   繁体   English

window.onload 不正确

[英]window.onload Not Working correclty

This is a simple function to show the date (I don't want professional ones so this is enough).这是一个简单的 function 来显示日期(我不想要专业的所以这就足够了)。 The problem is that I want to show it when the page loads.问题是我想在页面加载时显示它。 I don't want the user to press on any buttons or anything, but it's not working.我不希望用户按下任何按钮或任何东西,但它不起作用。

Here it is:这里是:

<script type="text/javascript">
    window.onload = ReturnDay;
</script>

<script>
    function ReturnDay() {
        var monthNames = [ 
            "January", 
            "February", 
            "March", 
            "April", 
            "May", 
            "June",
            "July", 
            "August", 
            "September", 
            "October", 
            "November", 
            "December" 
        ];
        var dayNames= [
            "Sunday",
            "Monday",
            "Tuesday",
            "Wednesday",
            "Thursday",
            "Friday",
            "Saturday"
        ];
        var newDate = new Date();

        newDate.setDate(newDate.getDate() + 1);    

        $('#dateday').html(dayNames[newDate.getDay()] + " " + newDate.getDate() + ' ' + monthNames[newDate.getMonth()] + ' ' + newDate.getFullYear());​
    }
</script>

<input id="dateday" type=text></div>

Any ideas?有任何想法吗?

Thanks a lot.非常感谢。

Function hoisting only takes place within a given <script> element. Function 提升仅发生在给定的<script>元素内。 When you attempt to assign ReturnDay to onload , it doesn't exist.当您尝试将ReturnDay分配给onload时,它不存在。

Remove lines 3 and 4 of your sample code:删除示例代码的第 3 行和第 4 行:

    </script>
     <script>

That said, assigning functions directly to event handler objects is not considered best practise.也就是说,将函数直接分配给事件处理程序对象并不是最佳做法。 It is too easy to introduce conflicts.引入冲突太容易了。 Use event binding instead, jQuery will abstract the differences in old-IE for you.改为使用事件绑定,jQuery 将为您抽象出旧 IE 中的差异。

$(window).load(ReturnDay);

or, if you want to run the script when the DOM is ready instead of waiting for all the images in the page to load:或者,如果您想在 DOM 准备就绪时运行脚本,而不是等待页面中的所有图像加载:

$(document).ready(ReturnDay);

Also, <input> elements are defined as EMPTY.此外, <input>元素被定义为 EMPTY。 Setting their innerHTML makes no sense.设置他们的 innerHTML 是没有意义的。 You should use val() not html() .您应该使用val()而不是html()

Although you could be clearer on the issue, I think the problem's because you're trying to set the html of an input field.尽管您可以更清楚地了解这个问题,但我认为问题是因为您正在尝试设置输入字段的 html。 Try this instead试试这个

$('#dateday').val(...);

its better to use jquery $(document).ready() instead of window.onload最好使用 jquery $(document).ready()而不是window.onload

The main problem is that you should be setting the value of the input, not it's innerHTML.主要问题是您应该设置输入的值,而不是 innerHTML。

document.getElementById('dateday').value = dayNames.......;

That said, I don't think your function is defined before you try to assign it to onload.就是说,我不认为您的 function 在您尝试将其分配给 onload 之前已定义。 I know functions are hoisted, but I think that only applies to the current code block.我知道函数被提升了,但我认为这只适用于当前代码块。

You don't need to define a named function anyway.无论如何,您不需要定义一个命名的 function。 Just do this:只需这样做:

window.onload = function() {
    // your date stuff here
};

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

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