Нажмите кнопку - PullRequest
       0

Нажмите кнопку

0 голосов
/ 26 апреля 2020

Понятия не имею, что я здесь делаю неправильно, но кнопка click me не работает, почему? Я хочу, чтобы при первом щелчке мышью менялся цвет первого, а при втором щелчке по мне менялся цвет второго абзаца. В любом случае, клик не работает. Я должен посмотреть на свои функции, но не вижу, что я сделал неправильно. Может быть, нужно создать другую функцию, которая заставит их работать? Вот и все, нажатие кнопки не работает с этим кодом.

<!DOCTYPE html>
    <html>
    <head>
    <h1>Tv Shows</h1>


    <div class="bg-image img1">
      <p id="demo">My Top 5 Tv shows,is Prison Break,Wayward Pines,Mentalist,Lost,Usa shooter</p>
      <button onclick="myFunction()">click me</button>
    </div>
    <div class="bg-image img2">
      <p id="demo1">My Top 5 movies,is Run all Night ,Asterix and Obelix,,Lost,Usa
      shooter</p>
      <button onclick="myFunction()">click me</button>
    </div>
    <style>

    body, html {
      height: 100%;
      margin: 0;
      font-family: Arial, Helvetica, sans-serif;
    }

    * {
      box-sizing: border-box;
    }

    .bg-image {
      /* Full height */
      height: 50%; 

      /* Center and scale the image nicely */
      background-position: center;
      background-repeat: no-repeat;
      background-size: cover;
    }

    /* Images used */
    .img1 { background-image: url("https://images.pexels.com/photos/1115804/pexels-photo-1115804.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260"); }
    .img2 { background-image: url("https://images.pexels.com/photos/3989816/pexels-photo-3989816.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260"); }


    /* Position text in the middle of the page/image */
    .bg-text {
      background-color: rgb(0,0,0); /* Fallback color */
      background-color: rgba(0,0,0, 0.4); /* Black w/opacity/see-through */
      color: white;
      font-weight: bold;
      font-size: 80px;
      border: 10px solid #f1f1f1;
      position: fixed;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      z-index: 2;
      width: 300px;
      padding: 20px;
      text-align: center;
    }
    function myFunction() {
      var x = document.getElementById("demo");
      x.style.fontSize = "25px"; 
      x.style.color = "red"; 
    }

    function mySec() {
      var x = document.getElementById("demo1");
      x.style.fontSize = "25px"; 
      x.style.color = "green"; 
    }

    </style>
    </head>
    <body>



    </body>
    </html>

Ответы [ 4 ]

1 голос
/ 26 апреля 2020

Прежде всего синтаксис, использованный в коде, неверен. Лучший способ - использовать одну функцию и передать идентификатор тега <p>. Ниже обновленный код

<html>

<head>

    <style>
        body,
        html {
            height: 100%;
            margin: 0;
            font-family: Arial, Helvetica, sans-serif;
        }

        * {
            box-sizing: border-box;
        }

        .bg-image {
            /* Full height */
            height: 50%;
            /* Center and scale the image nicely */
            background-position: center;
            background-repeat: no-repeat;
            background-size: cover;
        }
        /* Images used */

        .img1 {
            background-image: url("https://images.pexels.com/photos/1115804/pexels-photo-1115804.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260");
        }

        .img2 {
            background-image: url("https://images.pexels.com/photos/3989816/pexels-photo-3989816.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260");
        }
        /* Position text in the middle of the page/image */

        .bg-text {
            background-color: rgb(0, 0, 0);
            /* Fallback color */
            background-color: rgba(0, 0, 0, 0.4);
            /* Black w/opacity/see-through */
            color: white;
            font-weight: bold;
            font-size: 80px;
            border: 10px solid #f1f1f1;
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            z-index: 2;
            width: 300px;
            padding: 20px;
            text-align: center;
        }
    </style>
</head>

<body>
    <h1>Tv Shows</h1>
    <div class="bg-image img1">
        <p id="demo">My Top 5 Tv shows,is Prison Break,Wayward Pines,Mentalist,Lost,Usa shooter</p>
        <button onclick="myFunction('demo')">click me</button>
    </div>
    <div class="bg-image img2">
        <p id="demo1">My Top 5 movies,is Run all Night ,Asterix and Obelix,,Lost,Usa shooter
        </p>
        <button onclick="myFunction('demo1')">click me</button>
    </div>

    <script>
        function myFunction(id) {
            var x = document.getElementById(id);
            x.style.fontSize = "25px";
            x.style.color = "red";
        }
    </script>
</body>

</html>
0 голосов
/ 26 апреля 2020

Вы бы попробовали это так? Я отредактировал неправильные теги.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <style>
        body,
        html {
            height: 100%;
            margin: 0;
            font-family: Arial, Helvetica, sans-serif;
        }

        * {
            box-sizing: border-box;
        }

        .bg-image {
            /* Full height */
            height: 50%;

            /* Center and scale the image nicely */
            background-position: center;
            background-repeat: no-repeat;
            background-size: cover;
        }

        /* Images used */
        .img1 {
            background-image: url("https://images.pexels.com/photos/1115804/pexels-photo-1115804.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260");
        }

        .img2 {
            background-image: url("https://images.pexels.com/photos/3989816/pexels-photo-3989816.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260");
        }


        /* Position text in the middle of the page/image */
        .bg-text {
            background-color: rgb(0, 0, 0);
            /* Fallback color */
            background-color: rgba(0, 0, 0, 0.4);
            /* Black w/opacity/see-through */
            color: white;
            font-weight: bold;
            font-size: 80px;
            border: 10px solid #f1f1f1;
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            z-index: 2;
            width: 300px;
            padding: 20px;
            text-align: center;
        }
    </style>
</head>

<body>
    <h1>Tv Shows</h1>
    <div class="bg-image img1">
        <p id="demo">My Top 5 Tv shows,is Prison Break,Wayward Pines,Mentalist,Lost,Usa shooter</p>
        <button onclick="myFunction()">click me</button>
    </div>
    <div class="bg-image img2">
        <p id="demo1">My Top 5 movies,is Run all Night ,Asterix and Obelix,,Lost,Usa
            shooter</p>
        <button onclick="myFunction()">click me</button>
    </div>

    <script>
        function myFunction() {
            var x = document.getElementById("demo");
            x.style.fontSize = "25px";
            x.style.color = "red";
        }

        function mySec() {
            var x = document.getElementById("demo1");
            x.style.fontSize = "25px";
            x.style.color = "green";
        }
    </script>
</body>

</html>
0 голосов
/ 26 апреля 2020
<!DOCTYPE html>
<html>
    <head>
        <style>
body, html { height: 100%; margin: 0; font-family: Arial, Helvetica, sans-serif; } * { box-sizing: border-box; } .bg-image { /* Full height */ height: 50%; /* Center and scale the image nicely */ background-position: center; background-repeat: no-repeat; background-size: cover; } /* Images used */ .img1 { background-image: url("https://images.pexels.com/photos/1115804/pexels-photo-1115804.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260"); } .img2 { background-image: url("https://images.pexels.com/photos/3989816/pexels-photo-3989816.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260"); } /* Position text in the middle of the page/image */ .bg-text { background-color: rgb(0,0,0); /* Fallback color */ background-color: rgba(0,0,0, 0.4); /* Black w/opacity/see-through */ color: white; font-weight: bold; font-size: 80px; border: 10px solid #f1f1f1; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); z-index: 2; width: 300px; padding: 20px; text-align: center; }
</style>

<script> function myFunction() { var x = document.getElementById("demo"); x.style.fontSize = "25px"; x.style.color = "red"; } function mySec() { var x = document.getElementById("demo1"); x.style.fontSize = "25px"; x.style.color = "green"; }
</script>
    </head>
    <body>
        <h1>Tv Shows</h1>
        <div class="bg-image img1">
            <p id="demo">My Top 5 Tv shows,is Prison Break,Wayward Pines,Mentalist,Lost,Usa shooter</p>
            <button onclick="myFunction()">click me</button>
        </div>
        <div class="bg-image img2">
            <p id="demo1">My Top 5 movies,is Run all Night ,Asterix and Obelix,,Lost,Usa shooter</p>
            <button onclick="myFunction()">click me</button>
        </div>
    </body>
</html>

Вы вложили код Javascript в теги <style>, они должны быть заключены в теги <script>

0 голосов
/ 26 апреля 2020

Просто для использования тега script какой-нибудь код / ​​скрипт

    function myFunction() {
      var x = document.getElementById("demo");
      x.style.fontSize = "25px"; 
      x.style.color = "red"; 
    }

    function mySec() {
      var x = document.getElementById("demo1");
      x.style.fontSize = "25px"; 
      x.style.color = "green"; 
    }
<!DOCTYPE html>
    <html>
    <head>
 
    <h1>Tv Shows</h1>


    <div class="bg-image img1">
      <p id="demo">My Top 5 Tv shows,is Prison Break,Wayward Pines,Mentalist,Lost,Usa shooter</p>
      <button onclick="myFunction()">click me</button>
    </div>
    <div class="bg-image img2">
      <p id="demo1">My Top 5 movies,is Run all Night ,Asterix and Obelix,,Lost,Usa
      shooter</p>
      <button onclick="mySec()">click me</button>
    </div>
    <style>

    body, html {
      height: 100%;
      margin: 0;
      font-family: Arial, Helvetica, sans-serif;
    }

    * {
      box-sizing: border-box;
    }

    .bg-image {
      /* Full height */
      height: 50%; 

      /* Center and scale the image nicely */
      background-position: center;
      background-repeat: no-repeat;
      background-size: cover;
    }

    /* Images used */
    .img1 { background-image: url("https://images.pexels.com/photos/1115804/pexels-photo-1115804.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260"); }
    .img2 { background-image: url("https://images.pexels.com/photos/3989816/pexels-photo-3989816.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260"); }


    /* Position text in the middle of the page/image */
    .bg-text {
      background-color: rgb(0,0,0); /* Fallback color */
      background-color: rgba(0,0,0, 0.4); /* Black w/opacity/see-through */
      color: white;
      font-weight: bold;
      font-size: 80px;
      border: 10px solid #f1f1f1;
      position: fixed;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      z-index: 2;
      width: 300px;
      padding: 20px;
      text-align: center;
    }
     </style>
  
   
    </head>
    <body>



    </body>
    </html>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...