'var'问题 [英] 'var' question
问题描述
我使用以下内容每天都会显示不同的图片....
< script language =" JavaScript">
<! -
function adspic(){
var mydate = new Date()
var day = mydate.getDay()
var month = mydate.getMonth()
var daym = mydate.getDate()
if(daym< 10 )
daym =" 0" + daym
var adspicarray = new Array(" ads / images / kilt.jpg",
" ads / images / kilt.jpg",
" ads / images / kilt.jpg",
" ads / images / apronandcase.jpg",
" ads / images / kilt.jpg",
" ads / images / apronandcase.jpg",
" ads / images /apronandcase.jpg")
document.write("< p align =''right''>< img src =''" + adspicarray [day] +
"''/>")}
// - >
< / script>
问题(1)
我该如何做它是随机的?
问题(2)
如何制作它以便[day]可以用于其他函数(目前我在每个函数中使用相同的''var''序列
I am using the following for a different picture to show up every day....
<script language="JavaScript">
<!--
function adspic() {
var mydate=new Date()
var day=mydate.getDay()
var month=mydate.getMonth()
var daym=mydate.getDate()
if (daym<10)
daym="0"+daym
var adspicarray=new Array("ads/images/kilt.jpg",
"ads/images/kilt.jpg",
"ads/images/kilt.jpg",
"ads/images/apronandcase.jpg",
"ads/images/kilt.jpg",
"ads/images/apronandcase.jpg",
"ads/images/apronandcase.jpg")
document.write("<p align=''right''><img src = ''" + adspicarray[day] +
"''/>")}
// -->
</script>
Question (1)
How do I do it so that it is random??
Question (2)
How do i make it so that [day] can be used in other functions (currently i
use the same ''var'' sequence in each function
推荐答案
>我该如何做它是随机的?
它将类似于以下未经测试的代码。
< script type =''text / javascript''>
function adspic(){
var adspicarray = [" ads / images / kilt.jpg",
" ads / images / kilt.jpg",
" ads / images / kilt.jpg",
" ads / images / apronandcase。 jpg",
" ads / images / kilt.jpg",
" ads / images / apronandcase.jpg",
" ; ads / images / apronandcase.jpg"];
document.write("< p align =''right''>< img src =''" +
adspicarray [Math.floor(7 * Math.random())] +"''/>");
}
< br $> b $ b< / script>
不要理会它被弃用的语言属性。使用它们
类型属性。还有<!---->没有必要了
显然。在你的
语句结尾处也不要忘记分号。您也可以使用数组文字表示法而不是新的
数组()。尝试将您的orignial代码放入 http://www.jslint.com 中以获取
关于改进语法的建议。
彼得
> How do I do it so that it is random??
It will be something like the following untested code.
<script type=''text/javascript''>
function adspic() {
var adspicarray=["ads/images/kilt.jpg",
"ads/images/kilt.jpg",
"ads/images/kilt.jpg",
"ads/images/apronandcase.jpg",
"ads/images/kilt.jpg",
"ads/images/apronandcase.jpg",
"ads/images/apronandcase.jpg"];
document.write("<p align=''right''><img src = ''" +
adspicarray[Math.floor(7*Math.random())] +"''/>");
}
</script>
Don''t bother with the language attribute it is deprecated. Use they
type attribute instead. Also the <!----> is not necessary anymore
apparently. Also don''t forget semi-colons at the end of your
statements. Also you can use array literal notation instead of new
Array(). Try putting your orignial code in http://www.jslint.com for
suggestions on improving syntax.
Peter
JimK写道:
var day = Math.round(Math.random()* 5)
day是星期几的变量(0 =星期日 - 6 =星期六)。
var day = Math.round(Math.random() * 5)
day is variable of the day of the week (0 = Sunday - 6 = Saturday).
随着这个随机数生成,你永远不会生成6.另外0
和5将出现1,2,3的频率的一半,4。
彼得
With this random number generation you will never generate a 6. Also 0
and 5 will occur with half the frequency of 1,2,3,4.
Peter
JimK写道:
JimK wrote:
var day = Math.round(Math.random()* 6)// 6张图片
day是星期几的变量(0 =星期日 - 6 =星期六)。
var day = Math.round(Math.random() * 6) // 6 images
day is variable of the day of the week (0 = Sunday - 6 = Saturday).
现在图片0和6的出现频率只有天数的一半
1,2,3,4,5
(0-0.5) - > 0
[0.5-1.5] - > 1
[1.5-2.5] - > 2
[2.5-3.5] - > 3
[3.5-4.5] - > 4
[4.5-5.5] - > 5
[5.5-6] - > 6
Now pictures 0 and 6 will appear with half the frequency of days
1,2,3,4,5
(0-0.5) --> 0
[0.5-1.5) --> 1
[1.5-2.5) --> 2
[2.5-3.5) --> 3
[3.5-4.5) --> 4
[4.5-5.5) --> 5
[5.5-6) --> 6
这篇关于'var'问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!