这里出什么问题了,为什么innerHTML无法正常工作? [英] Whats wrong here, why is innerHTML not working?

查看:91
本文介绍了这里出什么问题了,为什么innerHTML无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下为什么innerHTML在以下代码中不起作用:

I wanted to ask why innerHTML is not working in the following code:

document.getElementById('text').innerHTML = localStorage["mytext"];

带有id文本的元素是div元素.如果我将它与警报一起使用,localStorage会为我提供正确的字符串,错误可能是什么?

The element with the id text is a div element. localStorage is giving me the right String if I use it with alert, what could the error be?

推荐答案

在尝试引用div之前,请确保div存在,

Make sure the div exists before trying to reference it,

window.addEventListener("load",function() {
    document.getElementById('text').innerHTML = localStorage.getItem("mytext");
},false);

在进行任何修改之前,它将等待文档加载

That will wait for the document to load before doing any modifications

这篇关于这里出什么问题了,为什么innerHTML无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆