如何让html打印javascript函数的返回值? [英] How to get html to print return value of javascript function?
问题描述
函数produceMessage(){
var msg ='Hello< br />';
返回信息;
}
编辑:哎呀,太多答案没有我澄清我的意思。我知道如何通过脚本标记来完成它。但是,假设我想让消息变成红色。我只是将脚本标记包裹在我的css中,像这样?
< div style =color:red>< script> ; document.write(produceMessage())< / script>< / div>
我的主要问题是,你是否应该使用document.write来获取打印的返回值?
这样做。
其中一个是:
document.write(produceMessage() )
其他会以这种方式附加文档中的某个元素:
<$ p $ span $ {p>
var span = document.createElement(span);
span.appendChild(document.createTextNode(produceMessage()));
document.body.appendChild(span );
或者只是:
document.body.appendChild(document.createTextNode(produceMessage()));
如果您使用jQuery,您可以这样做:
$(document.body的).append(produceMessage());
What is the preferred syntax to get html to print return value of javascript function?
function produceMessage(){
var msg= 'Hello<br />';
return msg;
}
EDIT: Yikes, too many answers without me clarifying what I meant. I know how to do it via the script tag. However, let's say I wanted to make the message red. Would I just encase the script tag inside my css like so?
<div style="color:red><script>document.write(produceMessage())</script></div>
I guess my main question was, should you be using document.write to get the return values to print?
There are some options to do that.
One would be:
document.write(produceMessage())
Other would be appending some element in your document this way:
var span = document.createElement("span");
span.appendChild(document.createTextNode(produceMessage()));
document.body.appendChild(span);
Or just:
document.body.appendChild(document.createTextNode(produceMessage()));
If you're using jQuery, you can do this:
$(document.body).append(produceMessage());
这篇关于如何让html打印javascript函数的返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!