javascript或jquery:在一个警报中显示多个变量 [英] javascript or jquery: show multiple variables in one alert

查看:106
本文介绍了javascript或jquery:在一个警报中显示多个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

警告命令采用以下结构:

Alert command assumes this structure:

alert (variable)

如何在一个警报中显示多个变量?

How to show multiple variables in a single alert?

推荐答案


警告命令采用以下结构:警报(变量)

不, alert()假定这种结构:

    alert(some expression)

...其中some expression几乎是任何JavaScript表达式 - 如果表达式是不是一个字符串,它将被转换(虽然在某些情况下,例如,对于某些对象,结果可能不是很有意义)。

...where "some expression" is pretty much any JavaScript expression - if the expression is not a string it will be converted (though in some cases, e.g., for some objects the result might not be very meaningful).

所以:

alert(variable);
alert("string literal");
alert(variable1 + variable2 + variable3);
alert(variable1 + ", " + variable2);
alert(resultOfFunctionCall());
alert([1,2,3]);
alert(whatever() + "else" + you.can.think + "of");

甚至:

alert();   // displays "undefined"

请注意,如果您尝试调试代码,最好使用 console.log() than alert()。如果您正在尝试生成动态消息以显示用户只需根据需要连接变量,例如:

Note that if you are trying to debug your code you are better off using console.log() than alert(). If you are trying to produce a dynamic message to show the user just concatenate variables as needed, e.g.:

alert("Hello there " + name + ". Welcome.");

这篇关于javascript或jquery:在一个警报中显示多个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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