将 ID 替换为特定 URL 的元素

Replace ID to an element for a particular url

本文关键字:URL 元素 ID 替换      更新时间:2023-09-26

我在导航中有两个列表项:

<body id="trips">
   <nav>
      <li><a href="/trips.php" class="trips">Trips</a></li>
      <li><a href="/trips.php?offer=true" class="offers">Offers</a></li>
   </nav>
   ....
</body>

访问 /trip.php?offer=true 时,我需要将 <body id="trips"> 替换为 <body id="offers">,因为我正在使用 CSS 制作当前页面:

#trips.trips, #offers.offers { css for "current page" button }

知道吗?:)

试试这个,你需要在trips.php中添加这个

<?php
   $bodyID = 'trips'; // default value
   // when `offer` GET is set
   if(isset($_GET['offer']) && trim($_GET['offer']) == 'true'){
      $bodyID = 'offers';
   }
?>
<body id="<?php print $bodyID; ?>">
您还可以在

body 标记中添加脚本,这将设置所需的 id。

<script>
  document.body.id = location.href.split('?')[1].contains('offer=true');
</script>

如果你已经在使用 PHP,你可以确定 offer 的 GET 变量是否设置为 true,并相应地设置一个 id 变量:

if(isset($_GET['offer'])) { # If the GET variable is set
    if($_GET['offer'] == "true") { # If the GET variable is equal to true
        $id = "offers"; # Set our ID equal to offers
    }
}
else { # If the GET variable is not set
    $id = "trips"; # Set our ID equal to trips
}

然后,您将拥有以下内容的 HTML:

<body id="<?php if(isset($id)) { print $id; } ?>">