使用 Jquery 显示隐藏 DIV
我有这个简单的 JQuery 显示/隐藏函数,可以明显地显示和隐藏 div。 有三件事是我一个人无法完成的。
<script type="text/javascript">
$(document).ready(function() {
$('#showHideBox').show();
$('#showHidetoggle').click(function() {
$("#showHidetoggle").text("Show me")
$('#showHideBox').slideToggle(250);
return false;
});
});
</script>
<a href="#" id="showHidetoggle">Hide Me</a>
我正在寻找一种方法来更改锚标记上的文本以显示/隐藏。 我知道这个问题以前已经被问过很多次了。但我似乎无法找到我的脚本的具体示例,到目前为止,文本会在单击时发生变化,但不会在后续单击时发生变化。
脚本通过将 div 滑出视图来隐藏该 div,但是,我需要 div 上可见的一部分,这样我可以附加用户将单击的按钮(锚点)标签。
最后,当用户单击“隐藏”时,div 会滑出视图,仅当用户刷新页面时才会重新出现。如何使 div 保持隐藏状态,但仅在用户单击按钮时才显示?
我试图完成的示例可以在 constantcontact.com 上找到。查看页脚,您会看到它滑出视图,但按钮仍然可见。
任何帮助将不胜感激。
谢谢大家。
I have this simple JQuery Show/Hide function that obviously show and hide a div.
There are three things I cannot do on my own.
<script type="text/javascript">
$(document).ready(function() {
$('#showHideBox').show();
$('#showHidetoggle').click(function() {
$("#showHidetoggle").text("Show me")
$('#showHideBox').slideToggle(250);
return false;
});
});
</script>
<a href="#" id="showHidetoggle">Hide Me</a>
I am looking for a way to change the text on the anchor tag to say show/hide.
I know this has been asked before, many times. But I can't seem to find specific examples to my script, so far the text changes on click but not subsequent clicks.The script hides the div by sliding it out of view however, I need a portion on the div visible this way I can attached the button (anchor) tag that the user will click.
Finally, when the user clicks hide, the div slides out of view only to reappear when the user refreshes the page. How can I make the div stay hidden but only appear when the user click the button?
An example of what I am trying to accomplished is on this page can be found on constantcontact.com. Look at the footer, you'll see it slides out of view but the button is still visible.
Any help will be greatly appreciated.
Thanks everyone.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
因此,要实现此目的,最简单的方法(imo)是为按钮+框(您想要隐藏的)创建一个容器。您的按钮将始终保持不变。页面加载时,我们将一个事件附加到您的按钮,该事件将根据框的当前状态显示和隐藏您的框(如果隐藏,则显示它,如果可见,则隐藏它)。
很容易。
现在,您还希望在页面加载/页面访问之间保留可见/隐藏状态。为了实现这一点,您需要在用户的浏览器上设置一个 cookie(附注,如果您的用户已登录或其他情况,您可以通过其他方式执行此操作 - 但 cookie 是完成此操作的最简单方法,并且不会)不涉及服务器往返以将数据保存到数据库)。
为了做到这一点,我建议去获取 jQuery Cookie 插件 它的使用非常简单(正如您将在下面看到的那样),并消除了处理 cookie 时的许多麻烦。每次用户单击按钮并更改框的状态时,您都会向用户的浏览器写入一个 cookie 并存储框的当前状态。这样,当用户重新加载页面时,您将知道当前状态是什么(因为 cookie)。在下面的示例中,我将 cookie 设置为一年后过期,但您可以根据需要进行更改。
So, to accomplish this, the easiest way (imo) is to create a container for your button + box (that you want to hide). Your button will stay put, always. When the page loads, we attach an event to your button that will show and hide your box based on the box's current state (if it's hidden, show it and if it's visible, hide it).
Easy enough.
Now, you also want to persist that visible/hidden state between page loads / page visits. To accomplish this, you're going to need a cookie on the user's browser (side note, if your user is logged in or something, you can do this another way - but a cookie is the lightest way to get it done and doesn't involve a server round-trip to save data to your database).
In order to do this, I suggest going and getting the jQuery Cookie Plugin it's really simple to use (as you'll see below) and takes a lot of the headaches out of dealing with cookies. Every time your user clicks your button, and you change the state of the box, you're going to write a cookie to the user's browser and store the current state of the box. That way, when the user re-loads the page, you will know what the current state is (because of the cookie). In the example below, I set the cookie to expire in a year, but you can change that if you want.
我已经更改了以下幻灯片效果教程中的代码以替代一个
在 JQuery 单击事件上隐藏和显示锚标记。
以下代码的工作示例可以在此 JSFiddle 中看到:
I've altered the code from the following slide effect tutorial to alternate a
hide and show anchor tag on the JQuery click event.
A working example of the code below can be seen in this JSFiddle:
要保存状态,您需要使用服务器端代码或使用 cookie。
To save the state, you need to either use server side code or use a cookie.
应该做到这一点。
Should do the trick.
if ($('#showHideBox').is(':visible') == True) {/*change text*/} else {/*change text*/}
来更改根据需要显示/隐藏的文本。if ($('#showHideBox').is(':visible') == True) {/*change text*/} else {/*change text*/}
to change the text to show/hide as needed.如何隐藏和隐藏使用 Jquery & 显示标签java脚本
How to Hide & show tag using Jquery & java script