Javascript 如何在JS中的div内设置值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6310787/
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-08-23 21:13:24  来源:igfitidea点击:

How to set value inside div in JS

javascript

提问by GeekedOut

I have some code that looks like this:

我有一些看起来像这样的代码:

document.getElementById("error").style.display = 'block';

and when this happens I also want to display the error that is supposed to be shown which is stored in another JS variable. How can I add the value of that variable to the div which has id="error"

当发生这种情况时,我还想显示应该显示的错误,该错误存储在另一个 JS 变量中。如何将该变量的值添加到具有 id="error" 的 div

Thanks!

谢谢!

回答by Susam Pal

var errorMsg = "<p>Example error message</p>"
document.getElementById("error").innerHTML = errorMsg

回答by patapizza

document.getElementById("error").innerHTML = errMsg;

回答by Milan Aleksi?

document.getElementById("error").innerHTML = my_js_variable;

回答by epascarello

var errorDiv = document.getElementById("error");
errorDiv.innerHTML = yourVariable;
errorDiv.style.display = 'block';

回答by Shawn

document.getElementById('error').innerHTML = variable

document.getElementById('error').innerHTML = 变量

(using jQuery would be easier.

(使用jQuery会更容易。

回答by kinakuta

var displayDiv = document.getElementById('error');
displayDiv.style.display = 'block';
displayDiv.innerHTML = yourVariable;