函数在`if`语句中无法正常工作javascript / jQuery

时间:2018-12-16 11:48:27

标签: javascript jquery html

在if语句中调用函数时遇到问题。

无法正常工作的功能是drawRight(),它带有2个参数,这些参数会在循环中绘制div,文本和其他元素。

当我这样称呼我的网站时,它只是崩溃了,好像css没在应用。

    <div id="right">
        <script>
            $('.leftOption').click(function() {
                if(this.id == "option1") {
                    drawRight(3,5);
                }
            });
        </script>
    </div>

如果我在语句之外调用它,那么一切都很酷,但是无论是否调用语句,我都无法使用。 :(

    <div id="right">
        <script>
            $('.leftOption').click(function() {
                if(this.id == "option1") {
                }
            });
            drawRight(3,5);
        </script>
    </div>

drawRight()函数

let drawRight = (fromID, toID) => {
                for(var r=fromID; r<=toID; r++) {
                    if(r % 2 != 0) {
                        //nieparzyste
                        document.write(`<div id="set${r}" class="rightOption rightOptionColor1">`);
                    } else {
                        document.write(`<div id="set${r}" class="rightOption rightOptionColor2">`);
                    }

                        document.write(`<div class="r1">`);
                                document.write(`<div class="garage">`);
                                    document.write(`<img class="garage_icon" src="../assets/images/icons/icon_garage.png"/>`);
                                    document.write(`<div id="garage_header">`);
                                            document.write(`<div class="garage_text">`);
                                                    document.write(`Garaż`);
                                            document.write(`</div>`);
                                            document.write(`<div class="garage_id">`);
                                                    document.write(`(${r})`);
                                            document.write(`</div>`);
                                    document.write(`</div>`);
                                document.write(`</div>`);
                                document.write(`<div id="button${r}" class="button">`);
                                        document.write(`<div class="button_border">`);
                                                document.write(`<div class="button_text">`);
                                                        document.write(`Wejdź`);
                                                document.write(`</div>`);
                                        document.write(`</div>`);
                                document.write(`</div>`);
                        document.write(`</div>`);

                        document.write(`<div class="r2">`);
                                document.write(`<div class="owner_header">`);
                                        document.write(`<img class="owner_icon" src="../assets/images/icons/person.png">`);
                                        document.write(`<div class="owner_text">`);
                                            document.write(`Właściciel`);
                                        document.write(`</div>`);
                                        document.write(`<div id="owner${r}" class="owner_nick info_text">`);
                                                document.write(``);
                                        document.write(`</div>`);
                                document.write(`</div>`);
                        document.write(`</div>`);

                        document.write(`<div class="r3">`);
                                document.write(`<div class="parking_header">`);
                                        document.write(`<img class="parking_icon" src="../assets/images/icons/p.png">`);
                                        document.write(`<div class="parking_text">`);
                                            document.write(`Ilość miejsc`);
                                        document.write(`</div>`);
                                        document.write(`<div id="parking${r}" class="parking_quantity info_text">`);
                                                document.write(`5`);
                                        document.write(`</div>`);
                                document.write(`</div>`);
                        document.write(`</div>`);

                        document.write(`<div class="r4">`);
                                document.write(`<div class="rIcons">`);
                                    document.write(`<img id="rOwned${r}" class="rOwned rIcon" src="../assets/images/icons/owned.png"/>`);
                                    document.write(`<img id="rShared${r}" class="rShared rIcon" src="../assets/images/icons/shared.png"/>`);
                                document.write(`</div>`);
                        document.write(`</div>`);

                    document.write(`</div>`);
                }
            }

1 个答案:

答案 0 :(得分:0)

您可以尝试一下;

this.id == option1

我假设您的this.id是数字值,并且正在将其与字符串进行比较。

PS。我不知道这个option1来自哪里。请分享option1的来源。