如何使用javascript函数解决卡片不改变颜色

how to solve card does not change colour using javascript function

我想实现这个功能,当product_stock == 0时卡片元素会变成灰色。我尝试使用 javascript 实现此功能,但它不起作用。我不知道我的代码有什么问题。你们中的任何人都可以帮我解决这个问题吗?如果你们能向我展示有关如何解决此问题的代码示例,我们将不胜感激。任何帮助将不胜感激。谢谢!

这是我的 PHP 代码

    <?php
   $sql = "SELECT * FROM (( SELECT * FROM products) as YW1 
   LEFT JOIN 
   (SELECT product_id, SUM(quantity) totalquantity FROM ordered_items GROUP BY product_id) AS YW2 ON YW1.id = YW2.product_id) ORDER BY totalquantity DESC ";
  $query = $conn->query($sql);

  if (!mysqli_num_rows($query)) {
    echo '
      <div class="col-12">
        <div class="badge badge-danger">No Products Found</div>
      </div>
    ';
  } else {

    while ($row = $query->fetch_assoc()) {
      $product_stock =  $row['product_stock'];

    ?>
    <div class="col-lg-3 col-md-4 col-6 form-group" style="padding: 0 8px 0 8px;" >
        <div class="product-wrapper" id="productlist">
          <img class="product-img" loading="lazy" src="images/product-main/<?php echo $row['product_photo']; ?>" alt="">
          <div class="card-body" >
            <h5 class="product-title" style="min-height: 39px; text-decoration: none; width:150px; display: -webkit-box;-webkit-line-clamp: 2;-webkit-box-orient: vertical; overflow: hidden; text-align: left !important;"><?php echo $row['product_title']; ?></h5>
            <p class="product-price">RM<?php echo $row['product_price']; ?>/KG</p>
               <p style="font-size: 10px; margin-top:-10px; margin-bottom:-2px;" ><span class="text-danger" > <?php echo $sum = $row['totalquantity'] ?? 0;?> SOLD </span></p>
                <p style="font-size: 10px;" ><span class="text-success"><?php echo $row['product_stock']; ?> IN STOCK </span></p>
            <a href="product.php?cid=<?php echo $row['id']; ?>" class="btn btn-block addtocart text-center" style="text-decoration: none">View More</a>
          </div>
        </div>
    </div>
    <?php 
      }
    } 
  ?>

这是我在product_stock = 0

时用来把卡片变成灰色的javascript

已更新 JAVASCRIPT 代码

<script>
  getfocus();
function getfocus() {
  document.getElementById("productlist").focus();
    var product_stock = "<?php echo $product_stock; ?>";
    if(product_stock == 0) {
        $(product_stock).focus(); 
        $(product_stock).css({'border' : '1px solid red'});
        $(product_stock).css("background-color", "#d0d0d0");
    }
  }
</script>

这是因为在脚本中调用时product_stock变量未定义。 不要使用 javascript 代码,只需将样式直接添加到 PHP 代码即可。

 while ($row = $query->fetch_assoc()) {
  $product_stock =  $row['product_stock'];

 ?>
    <div class="col-lg-3 col-md-4 col-6 form-group" style="padding: 0 8px 0 8px;" >
        <div class="product-wrapper" id="productlist" style='<?php echo $product_stock == 0 ? "background-color:#d0d0d0; border:1px solid red;" : "";  ?>'>
            <img class="product-img" loading="lazy" src="images/product-main/<?php echo $row['product_photo']; ?>" alt="">
               <div class="card-body" >
                 <h5 class="product-title" style="min-height: 39px; text-decoration: none; width:150px; display: -webkit-box;-webkit-line-clamp: 2;-webkit-box-orient: vertical; overflow: hidden; text-align: left !important;"><?php echo $row['product_title']; ?></h5>
                 <p class="product-price">RM<?php echo $row['product_price']; ?>/KG</p>
                 <p style="font-size: 10px; margin-top:-10px; margin-bottom:-2px;" ><span class="text-danger" > <?php echo $sum = $row['totalquantity'] ?? 0;?> SOLD </span></p>
                 <p style="font-size: 10px;" ><span class="text-success"><?php echo $row['product_stock']; ?> IN STOCK </span></p>
                 <a href="product.php?cid=<?php echo $row['id']; ?>" class="btn btn-block addtocart text-center" style="text-decoration: none">View More</a>
               </div>
            </div>
        </div>
 <?php 
  }