gpt4 book ai didi

jquery - 隐藏(切换可见性)页面之间的元素

转载 作者:可可西里 更新时间:2023-11-01 13:11:25 32 4
gpt4 key购买 nike

我正在努力实现我不确定是否可行的事情。我想要一个切换按钮来隐藏/显示页面上的元素。使用 jQuery 很容易做到这一点。问题是我希望在打开的下一页上记住该设置。

我有 20 个页面显示网站上的价格。然后切换按钮应该隐藏/显示页面上的价格。而且我不想每次打开新页面时都切换,所以如果我将切换按钮设置为隐藏,价格应该在所有页面上隐藏,直到我再次点击按钮。

有人知道如何轻松完成此操作吗?这是一个 ASP 页面,因此设置 Session 变量是一种解决方案,但使用 jQuery 更酷,因为该元素会立即隐藏。

最佳答案

实现这一目标的方法没有尽头,但 Viridis 建议的方法听起来很可能是最好的方法。

  • 从 ASP session 变量初始化价格的可见性(默认为可见)
  • 使用 jQuery onClick 按钮显示/隐藏价格
  • 通过一个简单的 ASP 页面更新 ASP session 变量,通过同一按钮的 AJAX onClick 调用

这将是您的 asp 页面的简化(且未经测试!)版本:

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" lang="javascript" type="text/javascript"></script>
<script lang="javascript" type="text/javascript">

//page variable storing the show / hide flag, initialised from the session variable
<% if Session("hidePrices") = "Y" then %>
var hidePrices = true;
<% else %>
var hidePrices = false;
<%end if%>

//worker to actually show / hide the prices
function showHidePrices(pHidePrices)
{
if(pHidePrices)
$(".myPrices").hide();
else
$(".myPrices").show();
}

//show / hide prices button click handler
function showHidePricesOnClick()
{
//toggle the flag
hidePrices = !hidePrices;

//show / hide the prices
showHidePrices(hidePrices);

//toggle the flag stored in the session variable
$.ajax({
url: "http://www.yoursite.com/showHidePrices.asp",
cache: false,
success:function(result,status,xhr){
alert("Called showHidePrices.asp OK!");
},
error:function(xhr,status,error){
alert(xhr.responseText);
alert(status);
alert(error);
}
});
}

//hide the prices onload if necessary
$( document ).ready(function() {

if(hidePrices)
{
showHidePrices(true);
}
});

</script>

</head>
<body>

<p class="myPrices">price 1</p>
<p class="myPrices">price 2</p>
<p class="myPrices">price 3</p>

<input type="button" onclick="showHidePricesOnClick();" value="Show / Hide Prices"/>

</body>
</html>

showHidePrices.asp 页面中的代码,通过 AJAX 在 showHidePricesOnClick 方法中调用,类似于:

<%
if Session("hidePrices") = "Y" then
Session("hidePrices") = "N"
else
Session("hidePrices") = "Y"
end if
%>

正如我所说,这是未经测试的(我敢肯定可以做得更优雅),但希望能帮助您大致了解需要做什么才能实现您的目标。

关于jquery - 隐藏(切换可见性)页面之间的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20635631/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com