如何让html打印javascript函数的返回值? [英] How to get html to print return value of javascript function?

查看:737
本文介绍了如何让html打印javascript函数的返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 函数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屋!

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