Javascript 将ID替换为特定url的元素
我在导航中有两个列表项:Javascript 将ID替换为特定url的元素,javascript,php,jquery,html,css,Javascript,Php,Jquery,Html,Css,我在导航中有两个列表项: <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 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>
有什么想法吗?:) 试试这个,你需要在
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>
document.body.id=location.href.split(“?”)[1]。包含('offer=true');
如果您已经在使用PHP,您可以确定是否将GET变量for offer设置为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; } ?>">
<body id="<?php if(isset($id)) { print $id; } ?>">