如何获取html打印javascript函数的返回值?

时间:2022-08-27 20:42:45

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?

编辑:Yikes,太多的答案没有我澄清我的意思。我知道如何通过脚本标签来做到这一点。但是,假设我想让消息变红。我是否只是将脚本标签包含在我的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来获取要打印的返回值?

7 个解决方案

#1


13  

There are some options to do that.

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

One would be:

一个是:

document.write(produceMessage())

文件撰写(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());

#2


11  

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();

#3


4  

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.

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

#4


2  

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

Is a good example.

是一个很好的例子。

#5


2  

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.

您可以将其与其他函数结合使用,以便在评估后具有函数写入内容。

#6


1  

you could change the innerHtml on an element

你可以改变一个元素的innerHtml

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

#7


1  

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.

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

#1


13  

There are some options to do that.

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

One would be:

一个是:

document.write(produceMessage())

文件撰写(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());

#2


11  

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();

#3


4  

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.

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

#4


2  

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

Is a good example.

是一个很好的例子。

#5


2  

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.

您可以将其与其他函数结合使用,以便在评估后具有函数写入内容。

#6


1  

you could change the innerHtml on an element

你可以改变一个元素的innerHtml

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

#7


1  

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.

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