Comment changer la couleur d'arrière-plan d'un div en utilisant jQuery ? [fermé]
P粉585541766
P粉585541766 2023-09-16 21:25:27
0
1
511

Je souhaite changer la couleur d'arrière-plan d'un div lors de l'exécution à l'aide de jquery. Le div s'affiche mais sa couleur d'arrière-plan ne change pas. Je ne sais pas ce qui ne va pas avec le code que j'ai écrit.

<html>
    <head>
        <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    </head>
    <body>
        <div id="color" style="width:100px;height:100px;background-color:grey;">Hello, world!</div>
        <script>
            function change_color()
            {
                r = Math.floor(Math.random()*256);
                g = Math.floor(Math.random()*256);
                b = Math.floor(Math.random()*256);

                rgb_ = "rgb(" + r + ", " + g + ", " + b + ")";

                $("#color").css("background-color", rgb_);
            }

            setInterval(change_color, 1000);
        </script>
    </body>
</html>

Je veux une valeur RVB décimale.

P粉585541766
P粉585541766

répondre à tous(1)
P粉854119263

Vous devez utiliser la fonction Math.floor à la place de floor (floor n'est pas défini dans le code actuel, c'est pourquoi vos valeurs r g b sont invalides) :

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<html>
<head>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<body>
    <div id="color" style="width:100px;height:100px;background-color:grey;">Hello, world!</div>

    <script>
        function change_color() {
            var r = Math.floor(Math.random() * 256);
            var g = Math.floor(Math.random() * 256);
            var b = Math.floor(Math.random() * 256);
            var rgb_ = "rgb(" + r + ", " + g + ", " + b + ")";
            $("#color").css("background-color", rgb_);
        }

        setInterval(change_color, 1000);
    </script>
</body>
</html>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!