回显一个 javascript 变量

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

echo a javascript variable

javascriptechoconsole-output

提问by gatuso

What is the best way to echo the variable get_parestotal?

回显变量 get_parestotal 的最佳方法是什么?

Some help please! Thank you

请一些帮助!谢谢

  <script type="text/javascript">
 $(document).ready(function(){
                    var get_parestotal = 0
                $(".parestotal").each(function(){
                    get_parestotal += parseFloat($(this).text());
                });
                alert(get_parestotal);

            });

</script>



<? echo json_encode($get_parestotal); ?>

回答by mhx

You can log variables and so on in the console.

您可以在控制台中记录变量等。

e.g.:

例如:

console.log(get_parestotal);

You can even concatenate witht he use of +console.log('This is your variable'+get_parestotal)

您甚至可以使用+console.log('This is your variable'+get_parestotal) 进行连接

You can even add styling to your log (color, background-color,...):

您甚至可以为日志添加样式(颜色、背景颜色……):

console.log('%cThis is your variable'+get_parestotal+'!','color:green;');

There are some alternatives to console.log() you could use:

您可以使用 console.log() 的一些替代方法:

 console.warn();
 console.info();
 console.error();
 console.debug();

回答by Codex

You can either do alert(myvar)or console.log(myvar).

您可以执行alert(myvar)console.log(myvar)

Caveat: console.log is only supported in certain browsers...see the link.

警告:console.log 仅在某些浏览器中受支持...请参阅链接。

回答by CMS

if you want a console output than use console.log(get_parestotal);

如果你想要一个控制台输出而不是使用 console.log(get_parestotal);