javascript - 我可以使用 onload 函数 -Javascript 一次显示 2 个项目吗?

标签 javascript html jquery onload

我使用 onload 函数来显示页面打开的日期,并且它工作正常。 但是当我将用户名添加到这个函数中时,它没有显示任何内容(我希望用户名也显示在页面顶部。)

这是我正在使用的功能:

    window.onload = function () {
    var newDate = new Date();
    $('#dateday').val(newDate.getFullYear() + '-' + newDate.getMonth() + '-' + newDate.getDate()); // This is showed correctly 
    alert(localStorage["username"]); // It's alerted correctly
    $('#usernamethename').val(localStorage["username"]); //This is not displayed.

html代码:

    <h3 id="usernamethename"></h3>
    <input id="dateday" type= text></input>

你知道为什么不显示用户名吗?如果有任何更好的想法可以在页面顶部显示名称,如果您提到它们,我将非常感激。

非常感谢:)

最佳答案

试试这个:

$('#usernamethename').text(localStorage["username"]);

关于javascript - 我可以使用 onload 函数 -Javascript 一次显示 2 个项目吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10149886/

相关文章:

javascript - 在 AngularJS 中嵌入我个人资料中的 Instagram 最近照片

javascript - react 函数说 "is not a function"

html - 如何设置图标旁边的列表和段落的样式?

java - html 文件中未找到类异常 Applet.jar

jquery 和 css 下拉菜单不起作用

javascript - 日期选择器范围不起作用

javascript - 谷歌饼图 : Uncaught Error: Container is not defined

HTML5/Javascript 视频过滤

javascript - 用 iFrame 替换所有对象标签 - 第一次替换后停止 - Javascript

javascript - 检查是否选择了两个多选下拉列表