数组有什么问题

What's wrong with the array?

本文关键字:问题 什么 数组      更新时间:2023-09-26
<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>

每次鼠标悬停或移出时,我都会尝试更改字体,并且此功能具有head6.style.fontFamily = typeFont[3]但数组没有。

你得到NaN,因为Math.random是一个函数,它不能解析为一个数字。

你必须调用功能

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

请注意,只要数组正好有 10 个索引,这就可以了,但您通常希望使用数组长度而不是10