我使用了功能切换,如果我刷新页面,它会记住切换的位置。我尝试使用会话,但找不到更新会话的方法(当用户希望看到更多或更少时)。
这是我的代码:
<script language="javascript">
function toggle(showHideDiv, switchImgTag) {
var ele = document.getElementById(showHideDiv);
var imageEle = document.getElementById(switchImgTag);
if(ele.style.display == "block") {
ele.style.display = "none";
imageEle.innerHTML = '<img src="more.png"">';
}else {
ele.style.display = "block";
imageEle.innerHTML = '<img src="less.png">';
}
}
</script>
<?php
if(empty($_SESSION["toggle"])){
$_SESSION["toggle"] = 'show';
}?>
<a id="imageDivLink" href="javascript:toggle('contentDivImg', 'imageDivLink');"><img src="lesss.png"></a>Title
<?php if($_SESSION["toggle"] == 'show'){ ?>
<div id="contentDivImg" style="display: block;">
<?php
$_SESSION["toggle"] = 'hidden';
}
if($_SESSION["toggle"] == 'hidden'){ ?>
<div id="contentDivImg" style="display: none">
<?php $_SESSION["toggle"] = 'show';
} ?>
Try Show & hide
</div>