JQuery 一次交换一个元素的切换
我是 JQuery 新手,我想知道您是否对如何使用 JQuery 而不是 JScript 完成以下操作有任何想法:
您在顶部 div 中有一组 a:
<h3>
<a id="acer" href="#acerca">acerca</a> |
<a id="cur" href="#cursos">cursos y clases</a> |
<a id="cal" href="#calendario">calendario</a> |
<a id="con" href="#contacto">contacto</a>
</h3>
在它们下面,在同一个 div 中容器 div,有四个内容 div,一个接一个,每个都属于上面的每个 a:
<div id="acerca"></div>
<div id="cursos"></div>
<div id="calendario"></div>
<div id="contacto"></div>
现在,这里的想法是,我们首先关闭所有这些内容 div,除了一个,第一个:acerca,它是可见的给用户:
$(document).ready(function(){
$("#cursos,#calendario,#contacto").hide();
});
现在,使用顶部的 h3 a,我希望发生以下行为:
1.- 如果我单击与默认打开的项目 (acerca) 不同的项目,则关闭当前打开的项目并显示我是新的。 2.- 如果我单击已打开的同一项目 (acerca),则不会发生任何事情:必须始终有一个内容 div 始终打开。 3.- 如果可能的话,使用#anchors 来掩盖丑陋的“javascript:;” 旧时代的。
使用 JavaScript 的 onclick 函数(除了#3)非常简单,但是,我不知何故陷入了 JQuery 的困境。
非常感谢帮助 索特克拉
I'm new to JQuery and I'd like to know if you've got any idea regarding how to accomplish the following with JQuery instead of JScript:
You have a group of a's within a top div:
<h3>
<a id="acer" href="#acerca">acerca</a> |
<a id="cur" href="#cursos">cursos y clases</a> |
<a id="cal" href="#calendario">calendario</a> |
<a id="con" href="#contacto">contacto</a>
</h3>
And below them, inside the same container div, there's four content divs, one after the other, each belonging to each of the a's above:
<div id="acerca"></div>
<div id="cursos"></div>
<div id="calendario"></div>
<div id="contacto"></div>
Now, the idea here is that we start off by closing all of these content div's but one, the first: acerca, which is visible to the user:
$(document).ready(function(){
$("#cursos,#calendario,#contacto").hide();
});
Now, using the h3 a's at the top, I want the following behaviour to take place:
1.- If I click a different item than the one open by default (acerca), then close the currently open one and show me the new one.
2.- If I click the same item which is already open (acerca), then nothing happens: there must always be one content div open at all times.
3.- If possible, using #anchors to mask the ugly "javascript:;" of the old days.
This is very simple with the use of JavaScript's onclick function (save for #3) but, I'm somehow getting stuck with JQuery.
Help is greatly appreciated
Sotkra
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我会为所有 DIV 分配一个公共类,这样我就可以集体引用它们。 当选择其中一个锚点时,我将从所有 DIV 中删除任何“选定”类,然后将“选定”类应用于应显示的 DIV。 然后,我将隐藏所有不具有所选类的 DIV,并显示具有所选类的 DIV。 这样,如果您重新单击同一链接,您将不会出现任何闪烁,就像您只是隐藏所有 DIV 然后显示选定的 DIV 一样。
I'd assign a common class to all the DIVs so I could reference them collectively. When one of the anchors is selected, I'd remove any "selected" classes from all of the DIVs, then apply a "selected" class to the DIV that should be shown. I would then hide all DIVs that don't have the selected class and show the DIV that does have the selected class. This way if you reclick the same link you won't have any flashing as you would if you simply hid all the DIVs then display the selected one.
最简单的解决方案是手动为菜单的每个元素添加事件,然后使用“切换”隐藏或显示,您可以添加与显示隐藏相同的效果
另一种解决方案,在这种情况下,您可以添加菜单项而不更改菜单项jquery代码,前提是锚文本必须是div id。
The easiest solution is to add manually the events for every element of the menu and then hidden or showing with "toggle" on wich you can add the same effects as with show hidde
Another solution, in this case you can add menu items without changing the jquery code, with the requisite that the anchor text must be the div id.
tvanfosson 的想法略有不同。
A slightly different variation of the tvanfosson's idea.
从您的建议中汲取一些灵感后,我想到了这一点:
在这种情况下:
1.-您将如何自动化其他 div 的“定位”,这样您就不必单独命名它们? 数组?
不管怎样,谢谢,我会继续努力的。
After drawing up some inspiration from your suggestions, I came up with this:
In this case:
1.-How would you automate the 'targetting' of the other divs so you don't name them individually? arrays?
Thanks anyway, I'll keep working on it.
我现在甚至为“锚点菜单”本身添加了额外的功能/触摸。 就 JQuery 而言,整个事情看起来像这样。 不要介意示例中使用的愚蠢变量名 =) :
I now went as far as adding an extra functionality/touch to the 'anchors menu' itself. The whole thing, JQuery wise, looks like this. Nevermind the silly variable names used for the example =) :
伙计们,答案很好!
我做了一些调整,以便在切换 div 时获得一些动画...我使用了幻灯片效果,但它应该适用于任何类型的动画。
注意:此代码仅在您之前选择了可见的 div 时才有效,因为我正在使用
hide()
内的回调函数来激活show()
效果并让它变得漂亮和流畅......Great answers guys!
I made some adjustments to get some animation while toggling the divs...I used the the slide effect but it should work with any kind of animation.
Note: This code only works if you have a previously selected and visible div, because i am using the callback function inside
hide()
to activate theshow()
effect and get it nice and smooth...