我一直在努力解决一个关于设置默认li的棘手问题。
我试图实现的是将一个li及其相关页面设置为默认页面。当用户点击其他链接时,它会加载其他页面。
HTML标记:
<div id="productIntro">
<div id="leftNav">
<ul>
<li class="head">
<a href="javascript:void(0)" onclick="show('.$row["id"].',1);"> Pictures</b></a>
</li>
<li class="head">
<a href="javascript:void(0)" onclick="show('.$row["id"].',2);"> <b>Comments</b></a>
</li>
<li class="head">
<a href="javascript:void(0)" onclick="show('.$row["id"].',3);"> <b>Others</b></a>
</li>
</ul>
</div>
<div class="main">
</div>
</div>
JS:
function show(id, page) {
$('.main').load("loadProDetail.php?id=" + id + "&page=" + page, true);
}
jQuery(document).ready(function () {
$(document).on('click', '.head', function () {
$(".head").removeClass("selected");
$(this).toggleClass("selected");
});
});
loadProDetail PHP
<?php
$id = $_GET['id'];
$sql = "select * from product where id=$id ";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$page = $_GET['page'];
if($page == 1)
{
$html .= '<img src="./product_images/' . $row["name"] . '.jpg" width="150" border="2">';
}
if($page == 2)
{
$html .= 'No comments so far';
}
if($page == 3)
{
$html .= 'This page is under construction';
}
echo $html;
CSS: .selected{background-color:red;}
在我的例子中,我想把"图片"设置为默认页面,当用户点击评论时,它会加载相应的页面。背景颜色也应该改变
您的页面逻辑存在于您的PHP中,因此可以通过修改if
语句并将其改为if...else
来指定默认情况下应该显示哪个页面:
if ($page==2) {
$html .= 'No comments so far';
} else if ($page==3) {
$html .= 'This page is under construction';
} else {
$html .= '<img src="./product_images/'.$row["name"].'.jpg" width="150" border="2">';
}
上面实现的逻辑是这样的:如果"评论"或"其他"页面被请求,提供它们,但如果没有,则只提供"图片"页面。
因为你也想加载"图片"内容时,页面最初加载,你可以调用show()
函数在你的Javascript页面加载。只要正确设置了$row["id"]
,将以下行添加到现有的jQuery(document).ready(function () { ... }
应该可以工作:
show($row["id"], 1);