Javascript阵列警报

时间:2011-10-27 04:42:39

标签: javascript arrays popup alert

我是Javascript的新手。

我试图编码这四个按钮。我现在正在第二个。我编写了一个数组。但是当我点击按钮时,它会替换页面。我想在警告框中显示数组。

<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>

1 个答案:

答案 0 :(得分:59)

如果要将数组视为数组,可以说

alert(JSON.stringify(aCustomers));

而不是所有document.write s。

http://jsfiddle.net/5b2eb/

但是,如果你想干净地显示它们,你的弹出窗口中每行一个,这样做:

alert(aCustomers.join("\n"));

http://jsfiddle.net/5b2eb/1/