Javascript 如何让html打印javascript函数的返回值?

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

How to get html to print return value of javascript function?

javascripthtml

提问by user784637

What is the preferred syntax to get html to print return value of javascript function?

让html打印javascript函数的返回值的首选语法是什么?

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?

编辑:哎呀,太多的答案,我没有澄清我的意思。我知道如何通过脚本标签做到这一点。但是,假设我想让消息变红。我会像这样将脚本标签包含在我的 css 中吗?

<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?

我想我的主要问题是,您应该使用 document.write 来获取要打印的返回值吗?

采纳答案by Matías Fidemraizer

There are some options to do that.

有一些选项可以做到这一点。

One would be:

一种是:

document.write(produceMessage())

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:

如果你使用 jQuery,你可以这样做:

$(document.body).append(produceMessage());

回答by Maxx

It depends what you're going for. I believe the closest thing JS has to print is:

这取决于你要做什么。我相信 JS 必须打印的最接近的东西是:

document.write( produceMessage() );

However, it may be more prudent to place the value inside a span or a div of your choosing like:

但是,将值放置在您选择的跨度或 div 中可能更谨慎,例如:

document.getElementById("mySpanId").innerHTML = produceMessage();

回答by Joseph Marikle

if you really wanted to do that you could then do

如果你真的想这样做,你可以这样做

<script type="text/javascript">
    document.write(produceMessage())
</script>

Wherever in the document you want the message.

在文档中您想要消息的任何位置。

回答by John Humphreys - w00te

<script type="text/javascript">
    document.write("<p>" + Date() + "</p>");
</script>

Is a good example.

是一个很好的例子。

回答by Brombomb

Or you can tell javascript where to write it.

或者你可以告诉javascript在哪里写它。

<script type="text/javascript">
    var elem = document.getElementById('myDiv');
    var msg= 'Hello<br />';
    elem.innerHTML = msg;
</script>

You can combine this with other functions to have function write content after being evaluated.

您可以将此与其他函数结合起来,在评估后具有函数写入内容。

回答by pheoke

you could change the innerHtml on an element

您可以更改元素上的innerHtml

function produceMessage(){
    var msg= 'Hello<br />';
     document.getElementById('someElement').innerHTML = msg;
}

回答by Gustav Barkefors

Most likely you're looking for something like

很可能你正在寻找类似的东西

var targetElement = document.getElementById('idOfTargetElement');
targetElement.innerHTML = produceMessage();

provided that this is not something which happens on page load, in which case it should already be there from the start.

前提是这不是在页面加载时发生的事情,在这种情况下,它应该从一开始就已经存在。