如何声明五个变量并在自己的行上声明每个变量

时间:2011-02-06 06:09:36

标签: javascript variables

我需要对此有一点了解。在示例中,我知道我做错了什么,因为我无法在Windows屏幕中将其拉出来。这只是我拥有的五个变量中的一个。我必须提出另一个问题是javascript.css,还是仅仅是<script>我不理解js.css的不同用法或仅使用<script>谢谢。

var stock[0] ="Cisco";
var changenet[0] ="0.155 up 0.72%";
var lastsale[0] =$21.775;
document.write("<p><strong>stock[0]<strong>: " + stock[0] +"Cisco" + 
  changenet[0] +" Up"+ lastsale[0] +"to buy is at.<\/p>");

好的,我将向您展示我已经工作了几个小时但仍然无法将其拉到网页上......

<!DOCTYPE html PUBLIC"-//W3C//DTD Xhtml 1.0 Strict//EN"
"http://www.w3.org.TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Project 4-1</title>
</head>
<body>

    <script type="text/javascript">
    <!--HIDE FROM INCOMPATIBLE BROWERS
    var stock[0] ="Cisco";
    var changenet[0] ="0.155 up 0.72%";
    var lastsale[0] =$21.775;
    document.write("<p><strong>stock[0]<strong>: " + stock[0] +"Cisco" + changenet[0] +"    Up"+ lastsale[0] +"to buy is at.</p>");

    var stock[1] ="Microsoft";
    var changenet[1] ="0.085 down 0.78%";
    var lastsale[1] =$27.61;
    document.write("<p><strong>stcok1<strong>: " + stock[1] +"Microsoft" + changenet[1] +"  down"+ lastsale[1] +"to buy it at.</p>");

    var stock[2] ="intel";
    var changenet[1] ="0.085 down 0.78%";
    var lastsale[2] =$21.40;
    document.write("<p><strong>stock2<strong>: " + stock[2] +"Intel" + changenet[2] +" up"+ lastsale[2] +"to buy it at.</p>");


    var stock[3] ="NVIDIA";
    var changenet[2] ="0.0212 up 0.10%";
    var lastsale[3] =$24.908;
    document.write("<p><strong>stock3<strong>: " + stock[3] +"NVIDIA" + changenet[3] +" down"+ lastsale[3] +"to buy it at.</p>");



    var stock[4] ="apple";
    var changenet[4] ="4.1301 down 1.20%";
    var lastsale[4] =$340.1898;
   document.write("<p><strong>stock4<strong>: " + stock[4]+"apple" + changenet[4] +" down"+ lastsale[4] +"to buy it at.</p>");

    var stock[5] ="EA";
    var changenet[5] ="0.4 down 2.60%";
    var lastsale[5] =$17.62;
    document.write("<p><strong>stock5<strong>: " + stock[5] +"EA" + changenet[5] +" down"+ lastsale[5] +"to buy it at.</p>");


    //STOP HIDING FROM INCOMPATIBLE BROWERS-->
    </script>
</body>
</html>

3 个答案:

答案 0 :(得分:3)

你需要学习更好的语法:Thau's JavaScript Tutorial(小心它有点过时)

如果stock,changenet和lastsale不是数组,则不需要[0]。如果它们是数组,那么只有在第一次定义数组时才应该放置var。

不使用数组:

var stock = "Cisco",
    changenet = "0.155 up 0.72%",
    lastsale = 21.775; //amount in dollars
document.write("Stock: " + stock + ", Changenet: " + changenet + ", Last Sale: " + lastsale);

使用数组:

var stock = [],
    changenet = [],
    lastsale = [];
stock[0] = "Cisco";
changenet[0] = "0.155 up 0.72%";
lastsale[0] = 21.755;
document.write("Stock: " + stock[0] + ", Changenet: " + changenet[0] + ", Last Sale: " + lastsale[0]);

如果你打算做计算,我建议你为changenet变量做一些其他的事情,比如把它变成2个不同的变量,一个带十进制数,一个用于perentage。

答案 1 :(得分:2)

var stock[0]很奇怪,可能是不正确的Javascript语法。 要声明数组,请使用:var myArray = new Array();(旧样式)或var myArray = [];

例如,尝试:

var stock = [], changenet = [], lastsale = [];
stock[0] ="Cisco";
changenet[0] ="0.155 up 0.72%";
lastsale[0] = "$21.775"; // note that the $ needs to be part of a string!
document.write("<p><strong>stock[0]<strong>: " + stock[0] +"Cisco" + 
 changenet[0] +" Up"+ lastsale[0] +"to buy is at.<\/p>");

或者,更简洁的解决方案,因为您实际上并未在示例中将变量用作数组:

var stock = "Cisco",
    changenet = "0.155 up 0.72%",
    lastsale = "$21.775"; // note that the $ needs to be part of a string! 
document.write("<p><strong>stock<strong>: " + stock + "Cisco" + 
 changenet +" Up"+ lastsale +"to buy is at.<\/p>");

已添加我再次查看了您的document.write语句,我注意到您在字符串文字中包含了您的引用stock 。它不会那样工作。所以你可能意味着:

document.write("<p><strong>" + stock + "<strong>: " + stock + "Cisco" + 
 changenet +" Up"+ lastsale +"to buy is at.<\/p>");
// Instead of
document.write("<p><strong>stock<strong>: " + stock + "Cisco" + 
 changenet +" Up"+ lastsale +"to buy is at.<\/p>");

在回复评论时添加:

  

我需要声明5个变量,每个变量必须在线上。然后我必须为每个人写一份声明。

您可以声明变量并同时初始化它们。或者您可以先声明它们,然后再设置它们。声明它们时,您不必重复var语句,因为var语句一次可以与多个变量一起使用。例如

var stocks = [], // declaring a new array
    stock = "Cisco", // declaring a simple var (stock) and initializing it
    changenet; // declaring a simple var without initializing it.
    // all of the above are part of one var statement.

// or, the same as the above, but written as multiple statements:
var stocks = []; // declaring a new array
var stock = "Cisco"; // declaring a simple var (stock) and initializing it
var changenet; // declaring a simple var without initializing it.
// note that the statements end with the ;

changenet = "0.155 up 0.72%"; // assigning a value to an existing variable

答案 2 :(得分:1)

正如大家提到的,你可以用这种方式声明你的变量:

var stock = "EA",
    changenet = "0.4 down 2.60%",
    lastsale = $17.62;

但要小心,如果不小心你会用';'结束一行,那么其他变量将被声明为全局:

var stock = "EA", // will be local
    changenet = "0.4 down 2.60%"; // will be local
    lastsale = $17.62; // will be global

一个小技巧来帮助你,你可以用这种方式声明变量(这是我最喜欢的):

var stock = "EA"
  , changenet = "0.4 down 2.60%"
  , lastsale = $17.62;

现在你至少看到你的“逗号”,如果一行以“;”结尾然后你得到JS错误:

var stock = "EA"
  , changenet = "0.4 down 2.60%";
  , lastsale = $17.62; // Syntax Error

关于阵列/非阵列和声明的其他内容被其他人提及,所以我只是想补充他们的答案