Javascript 为什么可以';我不能让Math.random()和if/else语句的组合起作用吗?

Javascript 为什么可以';我不能让Math.random()和if/else语句的组合起作用吗?,javascript,jquery,html,Javascript,Jquery,Html,我希望JQuery方法.html根据Math.random()函数生成的值将放入中。if/else语句用于确定添加了的td 问题是,每当我重新加载页面时,总是添加到#右下角,而不管Math.random生成了什么值 这是我的密码: // script.js $(document).ready(function () { var value = Math.random; if (value <= 0.1) { $("#top-left").html("<

我希望JQuery方法
.html
根据
Math.random()
函数生成的值将
放入
中。if/else语句用于确定添加了
td

问题是,每当我重新加载页面时,
总是添加到
#右下角
,而不管
Math.random
生成了什么值

这是我的密码:

// script.js
$(document).ready(function () {
    var value = Math.random;
    if (value <= 0.1) {
        $("#top-left").html("<img src='file.png'>");
    } else if (value <= 0.2 && value > 0.1) {
        $("#top-middle").html("<img src='file.png'>");
    } else if (value <= 0.3 && value > 0.2) {
        $("#top-right").html("<img src='file.png'>");
    } else if (value <= 0.4 && value > 0.3) {
        $("#middle-left").html("<img src='file.png'>");
    } else if (value <= 0.5 && value > 0.4) {
        $("#middle-middle").html("<img src='file.png'>");
    } else if (value <= 0.6 && value > 0.5) {
        $("#middle-right").html("<img src='file.png'>");
    } else if (value <= 0.7 && value > 0.6) {
        $("#bottom-left").html("<img src='file.png'>");
    } else if (value <= 0.8 && value > 0.7) {
        $("#bottom-middle").html("<img src='file.png'>");
    } else {
        $("#bottom-right").html("<img src='file.png'>");
    }
});

<!-- page.html -->
<!DOCTYPE html>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
        <script type="text/javascript" src="script.js"></script>
    </head>
    <body>
        <table border="1px">
            <tr>
                <td class="square" id="top-left"></td>
                <td class="square" id="top-middle"></td>
                <td class="square" id="top-right"></td>
            </tr>
            <tr>
                <td class="square" id="middle-left"></td>
                <td class="square" id="middle-middle"></td>
                <td class="square" id="middle-right"></td>
            </tr>
            <tr>
                <td class="square" id="bottom-left"></td>
                <td class="square" id="bottom-middle"></td>
                <td class="square" id="bottom-right"></td>
            </tr>
        </table>
    </body>
</html>
//script.js
$(文档).ready(函数(){
var值=数学随机;
如果(值)
更改:

var value = Math.random;
致:

更改:

var value = Math.random;
致:

而不是

Math.random 
换成

Math.random()
否则,值只是一个函数引用,它将始终命中else语句,而不是

Math.random 
换成

Math.random()

否则,值只是一个函数引用,它将始终命中else语句。您可能还需要将数字向下舍入到一个小数位。您可能还需要将数字向下舍入到一个小数位。random是一个函数,需要调用
var value=Math.random();
学习使用console.log进行调试。
var value=Math.random;console.log(value);
还有if语句比它们需要的更复杂be@epascarello,如何简化if语句?无需检查之前的值,因为它已经进入if。
if(x
Math.random
是一个函数,需要调用
var-value=Math.random();
学会使用console.log调试。
var-value=Math.random;console.log(value)
另外,if语句比它们需要的复杂得多be@epascarello,我如何简化if语句?不需要检查前面的值,因为它已经进入了if。
if(xe)特别有趣,因为OP包含了()在标题中,但不在他的代码中。哦!特别有趣,因为OP在标题中包含()而不在他的代码中。哦!