gpt4 book ai didi

javascript - 如何声明五个变量并在其自己的行中声明每个变量

转载 作者:行者123 更新时间:2023-11-28 12:48:22 26 4
gpt4 key购买 nike

我需要对此有更多的了解。在我的例子中,我知道我做错了什么,因为我无法在 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>

最佳答案

您需要学习更好的语法: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个不同的变量,一个是十进制数,另一个是百分比。

关于javascript - 如何声明五个变量并在其自己的行中声明每个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4911914/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com