javascript .innerHTML 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19212104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:46:17  来源:igfitidea点击:

.innerHTML not working

javascriptinnerhtml

提问by user2089120

I have a very stupid problem. I would like to change the content of a table cell. My code:

我有一个非常愚蠢的问题。我想更改表格单元格的内容。我的代码:

        var x = ajax('...');

    alert(x);
    alert(document.getElementById(tdId).innerHTML);

    document.getElementById(tdId).innerHTML = x;

"x" has the right content => so ajax is working And the second alert(...) gives also the right (actual) content. But when I would like to write x in the cell, nothing happens. No error, nothing...

“x”具有正确的内容 => 所以 ajax 正在工作 第二个 alert(...) 也给出了正确的(实际)内容。但是当我想在单元格中写 x 时,什么也没有发生。没有错误,没有什么...

I checked some similar problems here, but without success.

我在这里检查了一些类似的问题,但没有成功。

I have a realy no idea, what can I test further to understand the problem. Do you have an idea? Thanks a lot for your help!

我真的不知道,我可以进一步测试什么来理解问题。你有想法吗?非常感谢你的帮助!

回答by user2089120

I've got the solution!!!

我有解决方案!!!

This is very stupid: I changed the content of the td by clicking on the td... but the new content has a button to click again... now, firefox starts from the beginning on and shows the "second" content again...

这很愚蠢:我通过单击 td 更改了 td 的内容...但新内容有一个按钮可以再次单击...现在,firefox 从头开始​​并再次显示“第二个”内容。 ..

Thats hard! :-)

太难了!:-)

However, I realy thank you very very much for your help! Sorry for wasting your time! :(

但是,我真的非常非常感谢您的帮助!抱歉浪费您的时间!:(

回答by Dementic

var x = "Hello World";
var td = document.getElementById("myId");
var text = document.createTextNode(x);
td.appendChild(text);

if your script is on the top of the page, the DOM has not finished loading when you try to run your javascript function.

如果您的脚本位于页面顶部,则当您尝试运行 javascript 函数时,DOM 尚未完成加载。

make sure your Javascript is running after the DOM has loaded:

确保在 DOM 加载后您的 Javascript 正在运行:

window.onload += function(){
    var x = "Hello World";
    var td = document.getElementById("myId");
    var text = document.createTextNode(x);
    td.appendChild(text);
}; 

or, move your script file to the bottom of the page. (the first is more recommened)

或者,将您的脚本文件移动到页面底部。(第一个更推荐)

回答by Victor Ferreira

your code is correct, but make sure this element is not hidden (with display: none or visibility: hidden, for example). if you set a default value in the HTML file, will it appear?

您的代码是正确的,但请确保此元素未隐藏(例如,显示:无或可见性:隐藏)。如果在 HTML 文件中设置了默认值,它会出现吗?

you can also make a test adding the content of xto another element. try document.body.innerHTML += xor even replace xto a string literal such as "this is a test".

您还可以进行将 的内容添加x到另一个元素的测试。尝试 document.body.innerHTML += x甚至替换x为字符串文字,例如“这是一个测试”。