这有什么不对吗? [英] Whats wrong with this?

查看:59
本文介绍了这有什么不对吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


< script type =" text / JavaScript" src =" here.js">

<! -

函数输出(){

var i = Math.round (5 * Math.random());

document.write(safetyTips [i]);

}

output(); < br $>
// - >

< / script>



safetyTips [] - 这个数组在here.js ...

解决方案

TheShadow1于2005年4月2日在comp.lang.javascript中写道


< ; script type =" text / JavaScript" src =" here.js">
<! -
函数输出(){
var i = Math.round(5 * Math.random());
document.write(safetyTips [i]);
}
输出();
// - >
< / script>




您不能同时使用外部和内联脚本

使用相同的< script>阻止。


不要使用<! - ,// - >,已经过时多年了。


-

Evertjan。

荷兰。

(用我的电子邮件地址替换所有带点的十字架)


TheShadow1写道:


< script type =" text / JavaScript" src =" here.js">
<! -
函数输出(){
var i = Math.round(5 * Math.random());
document.write(safetyTips [i]);
}
输出();
// - >
< / script>


safetyTips [] - 这个数组在here.js ...




Evertjan已经解决了你的主要问题,但你也通过硬编码(我推测)你阵列的长度

给自己一个维修问题。


鉴于你主题中问题的一般性质,我会加上

,你也可以使它更简洁(我已经使用了''s''作为名称

的safteyTips数组以避免包装):


< script type =" text / javascript" src =" here.js">< / script>

< script type =" text / javascript">

document.write(sT [ Math.round((sT.length-1)* Math.random())]);

< / script>


就是这样需要。


-

Rob


RobG说:


TheShadow1写道:


< script type =" text / JavaScript" src =" here.js">
<! -
函数输出(){
var i = Math.round(5 * Math.random());
document.write(safetyTips [i]);
}
输出();
// - >
< / script>


safetyTips [] - 这个数组在here.js ...



Evertjan已经解决了你的主要问题,但你也有通过硬编码(我假设)你的阵列的长度来给自己一个维护问题。

考虑到你主题中问题的一般性质,我会添加
你也可以使它更简洁(我已经使用''s''作为safteyTips数组的名称
以避免包装):

< script type =" text / javascript" src =" here.js">< / script>
< script type =" text / javascript">
document.write(sT [Math.round((sT.length) -1)* Math.random())]);




使用Math.round()会低于数组的第一个和最后一个元素。

sT [Math.floor(Math.random()* sT.length)]


<script type="text/JavaScript" src="here.js">
<!--
function output() {
var i = Math.round(5*Math.random());
document.write(safetyTips[i]);
}
output();
//-->
</script>

safetyTips[] - this array is in here.js ...

解决方案

TheShadow1 wrote on 02 apr 2005 in comp.lang.javascript:

<script type="text/JavaScript" src="here.js">
<!--
function output() {
var i = Math.round(5*Math.random());
document.write(safetyTips[i]);
}
output();
//-->
</script>



You cannot have both an external and inline scripting
with the same <script> block.

Don''t use <!--, //-->, outdated for years.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)


TheShadow1 wrote:

<script type="text/JavaScript" src="here.js">
<!--
function output() {
var i = Math.round(5*Math.random());
document.write(safetyTips[i]);
}
output();
//-->
</script>


safetyTips[] - this array is in here.js ...



Evertjan has already fixed your main problem, but you also have given
yourself a maintenance problem by hard-coding (I presume) the length
of your array.

Given the general nature of the question in your subject, I''ll add
that you can also make it more concise (I''ve used ''sT'' for the name
of the safteyTips array to avoid wrapping):

<script type="text/javascript" src="here.js"></script>
<script type="text/javascript">
document.write(sT[Math.round((sT.length-1)*Math.random())]);
</script>

is all that is required.

--
Rob


RobG said:


TheShadow1 wrote:

<script type="text/JavaScript" src="here.js">
<!--
function output() {
var i = Math.round(5*Math.random());
document.write(safetyTips[i]);
}
output();
//-->
</script>


safetyTips[] - this array is in here.js ...



Evertjan has already fixed your main problem, but you also have given
yourself a maintenance problem by hard-coding (I presume) the length
of your array.

Given the general nature of the question in your subject, I''ll add
that you can also make it more concise (I''ve used ''sT'' for the name
of the safteyTips array to avoid wrapping):

<script type="text/javascript" src="here.js"></script>
<script type="text/javascript">
document.write(sT[Math.round((sT.length-1)*Math.random())]);



Using Math.round() underselects the first and last elements of the array.
sT[Math.floor(Math.random()*sT.length)]


这篇关于这有什么不对吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆