jQuery 将 style.display 更改为从 PHP while 循环阻止在 div 类中



我已经为此工作了好几天。我有一个 PHP while 循环回显一个div shares 类,我已将 CSS 显示属性更改为 none,我的目标是!如果用户单击此共享类按钮,则div 类显示样式将更改为阻止。以下是我的HTML代码:

<section>
  <div class='showVideos'>
    <?php
        require_once'dbh.php';
        $obj = new dbh();
        $obj -> showVideos();   
    ?>                                
  </div>
  <img src="images/back.jpg" id='go-back-once' alt="" style='width:55px;height:55px;cursor:pointer;margin-left:155px;margin-top:35%;'>
  <img src="images/next.jpg" id='loadMoreVideos' alt="" style='width:55px;height:55px;cursor:pointer;margin-right:10px;margin-left:55px;margin-top:35%;'>                          
</section> 

抱歉,如果您不理解我的 HTML 代码,我使用的是 OOP 方法,所以我只是使用 PHP 从我的数据库中提取数据的showVideos()调用我的类方法。

下面是我的 PHP 函数从我的数据库中提取数据:

public function showVideos() {
      try{
      $con = new PDO("mysql:host=$this->serverhost;dbname=starstv;" , $this->serverusername, $this->serverpassword);
      $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $sql = $con -> prepare("SELECT * FROM videos ORDER BY date_uploaded DESC LIMIT 34");
      $sql -> execute();
      if($sql -> execute()) {
            $i = 0;
            $videoCount = $sql -> rowCount(); 
            echo "<div style='font-style:italic;font-weight:700;font-size:15px;margin-left:25px;color:green;'>videos&nbsp;&nbsp;&nbsp;&nbsp;<span style ='font-style:normal;color:black;'>".$videoCount."&nbsp;&nbsp;videos and more</span></div>";
            while($row = $sql -> fetch(PDO::FETCH_ASSOC)) {
                  $url = $row['url'];
                  $id = $row['video_id'];
                  $video_name = $row['video_name'];
                  $title = $row['video_title'];
                  $artist = $row['artist'];
                  $uploadedBy = $row['uploaded_by'];
                  $video_no = $row['video_no'];
                  $view = $row['views'];
                  $likes = $row['likes'];
                  $dislikes = $row['dislikes'];
                  $shares = $row['shares'];
                  $duration = $row['video_duration'];
                  $vid_thumbnail = $row['vid_thumbnail'];
                  $post_interval = (time() - ($row['date_uploaded'] ));
                  $post_duration = floor($post_interval/86400);
                  echo "<div class ='some-parent' style='margin-left:10px;margin-top:25px;float:left;background:mintcream;height:inherit;width:23.8%;position:relative;'>";
                  echo "<div><a href='index.php?video_id=$id&video_no=$video_no' style='text-decoration:none;'><img src='".$vid_thumbnail."' alt='thumbnail image' style='width:100%;height:150px;;'><span class='values' name ='ling' >"."<img src='images/images(62).jpg' style='width:20px;height:10px;position:absolute;bottom:108px;left:16px;color:#fff;font-size:15px;font-weight:bold;'>&nbsp;<span style='font-weight:bold;position:absolute;bottom:103px;left:40px;color:#fff;font-size:15px;'>".$view."</span></span><img src='images/images(167).jpg' style='width:15px;height:10px;position:absolute;bottom:106px;right:56px;'><div id='durTimeText' style='position:absolute;bottom:102px;right:16px;color:#fff;font-size:15px;font-weight:bold;'>$duration</div></a></div>";
                  echo "<div style='margin-top:-15px;font-weight:700;color:#000000;height:55px;margin-left:10px;'>".$title."<div style='margin-top:4px;text-align:center;font-weight:700;font-style:italic;font-size:15px;'>$post_duration </div> &nbsp;&nbsp;</div><br>";
                  echo '<input type="hidden" name = "hit'.$i.'" value = "'.$id.'" class = "name" />';
                  echo "&nbsp;&nbsp;<img src='images/like.jpg' class='take' id='okay' style='cursor:pointer;width:18px;height:15px;margin-bottom:-2px;'><span style='color:purple;font-weight:bold;'>&nbsp;".$likes."</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='images/dislike.jpg' class='give' id='okay' style='cursor:pointer;width:18px;height:15px;margin-bottom:-2px;'><span style='color:purple;font-weight:bold;'>&nbsp;".$dislikes."</span>";
                  echo '<input type="hidden" name = "like" value = "'.$id.'" class = "videolikes" style="cursor:pointer;border:none;background:transparent;height:25px;width:35px;"/>';
                  echo '<input type="hidden" name = "hit" value = "'.$id.'" class = "videodislikes" style="cursor:pointer;border:none;background:transparent;height:25px;width:55px;"/>';
                  echo '<input type="hidden" name = "hit" value = "'.$id.'" class = "videoshares" style="cursor:pointer;border:none;background:transparent;height:25px;width:55px;"/>';
                  echo "&nbsp;&nbsp;&nbsp;&nbsp;<span class='shares' style='width:25px;height:30px;cursor:pointer;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='images/share.jpg' alt='share image' style='width:35px;height:20px;margin-bottom:-4px;opacity:.6;'>&nbsp;<span style='color:purple;font-weight:bold;'>".$shares."</span></span>&nbsp;<div class='pull'></div><div id='myModal' class='modal' style='position:absolute;bottom:-20px;background:#ccc;'><div class='modal-content' style='position:absolute;bottom:-20px;background:#ccc;width:520px;margin:15px;display:none;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='images/images(213).jpg' alt='' style='width:45px;height:40px;cursor:pointer;'>&nbsp;&nbsp;<img src='images/images(215).jpg' alt='' style='width:45px;height:40px;cursor:pointer;'>&nbsp;&nbsp;<img src='images/images(216).jpg' alt='' style='width:45px;height:40px;cursor:pointer;'>&nbsp;&nbsp;<img src='images/images(217).jpg' alt='' style='width:45px;height:40px;cursor:pointer;'>&nbsp;&nbsp;<img src='images/images(218).jpg' alt='' style='width:45px;height:40px;cursor:pointer;'>&nbsp;&nbsp;<img src='images/images(219).jpg' alt='' style='width:45px;height:40px;cursor:pointer;'>&nbsp;&nbsp;<img src='images/images(220).jpg' alt='' style='width:45px;height:40px;cursor:pointer;'>&nbsp;&nbsp;<img src='images/images(223).jpg' alt='' style='width:45px;height:40px;cursor:pointer;'>&nbsp;&nbsp;<img src='images/images(225).jpg' alt='' style='width:45px;height:40px;cursor:pointer;'>&nbsp;&nbsp;<div style='background:#ffffff;color;#000000;margin:15px;height:25px;font-size:16px;'>$url</div><button id='close' style='float:right;background:ghostwhite;margin-right:15px;cursor:pointer;'>close</button></div></div>";
                  echo "</div>";
                  $i++;
            }
      }else {
            echo "Video not found";
            return false;
         }    
      }catch(PDOException $e) {
      throw new PDOException($e -> getMessage());
      }
   }

我的目标共享类在最后一个 PHP 回显中

这是我的JavaScriptc代码:

$(".shares").on('click',function() {
    var modal = document.getElementById('myModal');
    $(this).parent().children(".modal-content").style.display = 'block';
    $modal.style.display = 'block';
        });

但是它没有得到 id 和类,也没有更改显示以阻止。

是的,已经解决了,将 html 模式放在我的 dom 中,而不是从 php 循环调用它解决了我的问题,感谢您@Saptal快速响应

最新更新