[英]Hide a link when clicked on
我有一個頁面顯示帶有“ x個新評論”的鏈接。 然后,單擊要隱藏的鏈接。 當我單擊“后退”按鈕返回到我的頁面時,我希望該鏈接仍然被隱藏(我現在已經看到了注釋,因此它們不再是新的)。
可以使用簡單的javascript完成嗎? 由於性能原因,我不想重新加載原始頁面。
要在單擊時隱藏鏈接很簡單:
var a = document.getElementById("yourA");
a.onclick = function(){
this.style.display = "none";
return false;
};
要使其在下一頁加載時保持隱藏狀態,還需要進行大量工作。 隱藏鏈接后,您需要創建一個cookie,並在隨后加載頁面時讀取相同的cookie(並相應地隱藏)。
好吧,也許這會有所幫助。
根據您使用的編程語言,您將需要一個會話值。
例如,使用jQuery並假設您使用的是PHP:
<?php
//start the session
session_start();
//set the attribute
$_SESSION['hide'] = false;
?>
<script>
//function to hide all class='test' elements
function hide(h){
if(h){
$('.test').hide();
} else {
$('.test').show();
}
}
/*do this always when page loads
* verify with the value stored in session to hide or not the links
*/
window.onload = hide(<?php echo $_SESSION['hide']; ?>);
//onready
$(function() {
//when link class='test' is clicked
$('.test').click(function(){
//fadeOut or just $(this).hide();
$(this).fadeOut();
//set the session to hide = true
<?php $_SESSION['hide'] = true; ?>
});
});
</script>
//here goes your html
<body>
<a href="javascript:void(0);" title="Link" class="test">I am link 1</a>
<br/><br/>
<a href="javascript:void(0);" title="Link" class="test">I am link 2</a>
<br/><br/>
<a href="javascript:void(0);" title="Link" class="test">I am link 3</a>
<br/><br/>
<a href="javascript:void(0);" title="Link" class="test">I am link 4</a>
</body>
希望我的想法對您有用。
絕對可以使用基本的JavaScript完成。
通常,涉及以下步驟:
e
) e.target
屬性隱藏元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.