jQuery - 任何时候只显示一个 div
我正在开发一个新菜单,其中有多个隐藏的 div,但我只想一次在页面上显示一个 div。
这是我的代码; http://jsfiddle.net/sXqnD/
HTML 漂亮且简单;
<div id="linkwrapper">
<a id="link1" href="#">link1</a><br/>
<a id="link2" href="#">link2</a><br/>
<a id="link3" href="#">link3</a>
</div>
<div id="infocontent">
<div id="link1content">Information about 1.</div>
<div id="link2content">Information about 2.</div>
<div id="link3content">Information about 3.</div>
</div>
这是我对 jQuery 的尝试,它似乎运行得不太好。
$(document).ready(function(){
$('#infocontent').children().hide();
$('#linkwrapper a').click(function(){
var chosen1 = this.id;
$('#infocontent').children('div').each(function(i) {
var i = i+1;
if( $("#" + this.id).is(":visible") == true ) {
$("#" + this.id).hide(function(){
$("#" + chosen1 + "content").show();
});
return false;
} else {
$("#" + this.id).show();
return false;
}
});
});
});
谁能看到我哪里出了问题或者提出一个更优雅的解决方案?
I'm working on a new menu, where I have a multiple hidden divs, but I only want to show one div on the page at any one time.
Here is my code;
http://jsfiddle.net/sXqnD/
HTML is nice and simple;
<div id="linkwrapper">
<a id="link1" href="#">link1</a><br/>
<a id="link2" href="#">link2</a><br/>
<a id="link3" href="#">link3</a>
</div>
<div id="infocontent">
<div id="link1content">Information about 1.</div>
<div id="link2content">Information about 2.</div>
<div id="link3content">Information about 3.</div>
</div>
Here is my attempt at jQuery, which doesn't seem to play nicely.
$(document).ready(function(){
$('#infocontent').children().hide();
$('#linkwrapper a').click(function(){
var chosen1 = this.id;
$('#infocontent').children('div').each(function(i) {
var i = i+1;
if( $("#" + this.id).is(":visible") == true ) {
$("#" + this.id).hide(function(){
$("#" + chosen1 + "content").show();
});
return false;
} else {
$("#" + this.id).show();
return false;
}
});
});
});
Can anyone see where I have gone wrong or suggest a more elegant solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
$('div')
将查找网页上的所有 div。.filter
将在匹配$('div')
的结果中搜索并匹配具有 id=idOfDivToShow 的元素。.not
将在匹配$('div')
的结果中进行搜索,并匹配不具有 id=idOfDivToShow 的元素。最后,如果您只想在特定元素内搜索,例如#infocontent,那么您可以编写:
$('div')
will find all divs on your web page..filter
will search within the results that match$('div')
and match elements that have id=idOfDivToShow..not
will search within the results that match$('div')
and match elements that don't have id=idOfDivToShow.Finally, if you want to search only within a particular element, say #infocontent, then you could write:
我建议在点击功能中简化它,简单地隐藏所有内容,然后显示您想要的内容
I would suggest simplifying it in the click function to simply hide everything and then show the one you do want
这是一个与您的答案很接近的答案。
正是基于这样的思想:
- 查找div并仅显示指定的div(如果它是隐藏的)
- 隐藏所有其他div
This is an answer which is close to what you had.
It is based on this thought:
- find the div's and only show the specified div if it's hidden
- hide all other div's
我还需要花点时间将您的链接 href 属性更改为“javascript:void(null);”如果链接位于页面“折叠”下方,则可以防止页面跳转。
I would also take a moment to change your link href attributes to "javascript:void(null);" to prevent page jumping if the links are below the "fold" of the page.
这个怎么样? http://jsfiddle.net/sXqnD/2/
JS:
我想你会发现操纵的东西但从长远来看,使用类似格式的 ID 不会很好地使用。
How about this? http://jsfiddle.net/sXqnD/2/
JS:
I think you'll find manipulating stuff by similarly-formatted IDs will not be very nice to work with in the long run though.
为了最快的改变:
从
到
我基本上用 1)隐藏所有子项,然后 2)显示所需的 div 替换了 .each() 函数
For the quickest change:
From
To
I basically replaced the .each() function with 1) hiding all the children, and then 2) showing the desired div
我的解决方案在这里:
http://jsfiddle.net/sXqnD/8/
代码:
My solution is here:
http://jsfiddle.net/sXqnD/8/
Code:
感谢您的所有建议。
我发现这是我想要实现的目标的最佳解决方案。
http://jsfiddle.net/sXqnD/15/
HTML
jQuery
Thanks for all the advice.
I found this to be the best solution for what I was trying to achieve.
http://jsfiddle.net/sXqnD/15/
HTML
jQuery