Javascript 数组警报
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7911798/
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
Javascript Array Alert
提问by chrisholdren
Im new to Javascript.
我是 Javascript 新手。
Im trying to code these four buttons. I'm currently on the second one. I've coded an array. But when I click on the button, it replaces the page. I want to display the array in an alert box.
我正在尝试对这四个按钮进行编码。我目前在第二个。我已经编码了一个数组。但是当我点击按钮时,它会替换页面。我想在警报框中显示数组。
<html>
<head>
<script type="text/javascript">
function SayHello()
{
alert("Hello World!");
}
function DumpCustomers()
{
var aCustomers=new Array();
aCustomers[0]="Frank Sinatra ";
aCustomers[1]="Bob Villa ";
aCustomers[2]="Kurt Cobain ";
aCustomers[3]="Tom Cruise ";
aCustomers[4]="Tim Robbins ";
aCustomers[5]="Santa Claus ";
aCustomers[6]="Easter Bunny ";
aCustomers[7]="Clark Kent ";
aCustomers[8]="Marry Poppins ";
aCustomers[9]="John Wayne ";
document.write(aCustomers[0]);
document.write(aCustomers[1]);
document.write(aCustomers[2]);
document.write(aCustomers[3]);
document.write(aCustomers[4]);
document.write(aCustomers[5]);
document.write(aCustomers[6]);
document.write(aCustomers[7]);
document.write(aCustomers[8]);
document.write(aCustomers[9]);
}
function DisplayFishCounts()
{
}
function FindJonGalt()
{
}
</script>
</head>
<body>
<form name="Main">
<input type="button" id=1 onclick="SayHello();" value="Say Hi"/>
<input type="button" id=1 onclick="DumpCustomers();" value="Dump Customers"/>
<input type="button" id=1 onclick="DisplayFishCounts();" value="Display Fish Counts"/>
<input type="button" id=1 onclick="FindJonGalt();" value="Where is Jon Galt?"/>
</form>
</body>
</html>
回答by Ray Toal
If you want to see the array as an array, you can say
如果你想把数组看成一个数组,你可以说
alert(JSON.stringify(aCustomers));
instead of all those document.write
s.
而不是所有那些document.write
s。
However, if you want to display them cleanly, one per line, in your popup, do this:
但是,如果您想在弹出窗口中干净地显示它们,每行一个,请执行以下操作:
alert(aCustomers.join("\n"));