WebStorm 未在 Chrome 彈出視窗中執行 jQuery

WebStorm 未在 Chrome 彈出視窗中執行 jQuery

我正在使用 WebStorm 編寫一個網站,並運行我在 Overflow 上找到的一些程式碼,看看它是否能滿足我的目的。

超文本標記語言

<h2 class="quotes">first quote</h2>
<h2 class="quotes">second quote</h2>

CSS

.quotes {display: none;}

JavaScript

(function() {

    var quotes = $(".quotes");
    var quoteIndex = -1;

    function showNextQuote() {
        ++quoteIndex;
        quotes.eq(quoteIndex % quotes.length)
            .fadeIn(2000)
            .delay(2000)
            .fadeOut(2000, showNextQuote);
    }

    showNextQuote();

})();

它在它運行的 jsfiddle 上運行得很好,但是當我將它導入到 WebStorm 並在 Chrome 中運行它時,它不起作用。有小費嗎?

答案1

您試圖在元素存在之前取得它們,因此quotes陣列大小為 0 <script>

<!DOCTYPE html>
<html lang="en">
<head>
    <link href="https://fonts.googleapis.com/css?family=Fugaz+One&display=swap" rel="stylesheet">
    <meta charset="UTF-8">
    <title>Purple Reign</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

    <link rel="stylesheet" href="pvphs.css">
</head>
<body><h2 class="quotes">first quote</h2>
<h2 class="quotes">second quote</h2>
<script src="pvphs.js"></script>
</body>
</html>

相關內容