javascript 打印函数返回的值以在javascript中进行筛选

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

Print value returned by a function to screen in javascript

javascriptfunction

提问by bloomingsmilez

I wrote the following JavaScript functions :

我编写了以下 JavaScript 函数:

<script type="text/javascript">
function showVariable(val){         
    if(typeof(val)!=null && typeof(val)!=false &&typeof(val)!=NaN && typeof(val)!=undefined)
        return typeof(val);
    else 
        return val;
}
function print(){
    var val = {1, 2, 3};
    var res = showVariable(val);
    alert(res);
}
</script>

Currently, I can see the result using alert, but I want to know if there is another method to print showVariable's result in my html document.

目前,我可以使用警报查看结果,但我想知道是否有另一种方法可以showVariable在我的 html 文档中打印结果。

采纳答案by Matt Coughlin

Others have pointed out how to add text to an existing HTML element. A couple other options are described below.

其他人已经指出如何将文本添加到现有的 HTML 元素。下面描述了其他几个选项。

Error log

错误日志

For debugging, an alternative to alert()that's less intrusive is to add the text to the error log. For instance, in Firefox with the Firebug extension:

对于调试,另一种alert()侵入性较小的替代方法是将文本添加到错误日志中。例如,在带有 Firebug 扩展的 Firefox 中:

if (console.log) console.log(res);

Document.write

文件写入

Another option that probably won't apply to this particular question, but which is sometimes helpful, is to use document.write. Be careful not to use it after the page has loaded, however, or it will overwrite the page.

另一个可能不适用于此特定问题但有时有用的选项是使用document.write. 但是请注意不要在页面加载后使用它,否则它会覆盖页面。

For example, the following:

例如,以下内容:

<p>one</p>
<script type="text/javascript">document.write('<p>two</p>');</script>
<p>three</p>
<script type="text/javascript">document.write('<p>four</p>');</script>

Will be displayed in the browser as if the static HTML source code were:

将在浏览器中显示,就像静态 HTML 源代码是:

<p>one</p>
<p>two</p>
<p>three</p>
<p>four</p>


typeof

类型

On a side note, the typeof operator returns one of the following string values:

附带说明一下, typeof 运算符返回以下字符串值之一:

    'undefined'
    'null'          // For browsers that support ECMAScript 6+
    'boolean'
    'number'
    'string'
    'function'
    'object'

The initial if statement could be refactored as follows:

最初的 if 语句可以重构如下:

Instead of this               Use this                     Or this
-------------------           -----------------            ------------
typeof(val) != null           val !== null
typeof(val) != false          val !== false
typeof(val) != NaN            typeof val == 'number'       !isNaN(val)
typeof(val) != undefined      typeof val != 'undefined'

Not sure if you need all of those tests though. It depends on what you're trying to do.

不确定您是否需要所有这些测试。这取决于你想要做什么。

回答by sdespont

Simply use

只需使用

function print(){
    var val = {1, 2, 3};
    var res = showVariable(val);
    document.getElementById("myDiv").innerHTML = res;

   //if the target is an input :
   document.getElementById("myDiv").val = res;
}

回答by ehp

You can try document.write();

你可以试试 document.write();

function print(){
        var val = {1, 2, 3};
        var res = showVariable(val);
        document.write(res);
    }

回答by Arindam Roychowdhury

Most answers are correct. But for a beginner , it might be confusing . Let me break them into steps:

大多数答案是正确的。但是对于初学者来说,这可能会令人困惑。让我把它们分成几个步骤:

Problem: Lets say you want to collect the value from a text box and print it into the body of HTML.

问题:假设您想从文本框中收集值并将其打印到 HTML 正文中。

  • Step 1: Declare anything with an id 'result' (or anything)
  • Step 2: Collect from text box into a variable.
  • Step 3: Print it using innerHTML
  • 第 1 步:声明任何带有 id 'result'(或任何东西)的东西
  • 第 2 步:从文本框中收集到一个变量。
  • 第 3 步:使用 innerHTML 打印它

<html>
<body>

Hi everyone

<p id="result"></p>
<textarea cols="40" id="SearchText" rows="1"></textarea>

</div>
<button onclick="myFunction()" type="button">Submit!</button>
</div>
<script>
function myFunction() {
    var result = document.getElementById("SearchText").value;
 document.getElementById("result").innerHTML = 'hello' + result;
}
</script>

</div>

</body>
<html>

回答by Jason Sperske

You can set it to the .innerHTML property if an element. Like so:

如果是元素,您可以将其设置为 .innerHTML 属性。像这样:

<div id="output"></div>

document.getElementById('output').innerHTML = val;

回答by Konstantin Dinev

You can output to an element:

您可以输出到一个元素:

HTML:

HTML:

<div id="log"></div>

JavaScript:

JavaScript:

function print(value) {
    document.getElementById('log').innerHTML += value;
}