简体   繁体   English

数组出了什么问题?

[英]What's wrong with the array?

<html>

<head>

    <title> Random </title>

    <script type="text/javascript" language="JavaScript">

        var typeFont = new Array ( "cooper","Fixedsys","Edwardian Script ITC", "Gill Sans MT", "Kozuka Gothic Pro", "Lucida Sans", "Adobe Gothic Std", "Adobe Naskh", "Algerian","Arial Unicode MS");

        function font()
        {
            head6.style.fontFamily = typeFont[ Math.floor( Math.random * 10 ) ];
        }
    </script>
</head>

<body>

    <center>
        <h1 onmouseover="font()" onmouseout="font" id="head6" > this is the text </h1>
    </center>

</body>

I am trying to change the font every time the mouse is over or out and this function with head6.style.fontFamily = typeFont[3] but it doesn't with the array. 我试图每次鼠标head6.style.fontFamily = typeFont[3]head6.style.fontFamily = typeFont[3]时都更改字体,并且该功能与head6.style.fontFamily = typeFont[3]但它与数组无关。

You're getting NaN as Math.random is a function and it can't be parsed to a number. 由于Math.random是一个函数,因此无法解析为数字,因此您会得到NaN

You have to call the funtion 你必须打电话给功能

Math.floor( Math.random() * 10 ) 

Note that this is fine as long as the array have exactly 10 indices, but you'd generally want to use the arrays length instead of 10 请注意,只要数组具有恰好10个索引,就可以了,但是您通常希望使用数组长度而不是10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM