简化 Jquery 隐藏 Div
希望简化下面的 jquery 函数,该函数使用 click() 隐藏和显示数字 div,可以使用 next(),但不熟悉实现它的方法。
$(function() {
$('a#deliveryLink').addClass('selected');
$('#sizeguideDiv, #returnsDiv, #currencyDiv').hide();
$('a#sizeguideLink').click(function() {
$(this).addClass('selected');
$('a#deliveryLink, a#returnsLink, a#currencyLink').removeClass('selected');
$('#sizeguideDiv').show();
$('#deliveryDiv, #returnsDiv, #currencyDiv').hide();
return false;
});
$('a#returnsLink').click(function() {
$(this).addClass('selected');
$('a#deliveryLink, a#sizeguideLink, a#currencyLink').removeClass('selected');
$('#returnsDiv').show();
$('#sizeguideDiv, #deliveryDiv, #currencyDiv').hide();
return false;
});
$('a#deliveryLink').click(function() {
$(this).addClass('selected');
$('a#returnsLink, a#sizeguideLink, a#currencyLink').removeClass('selected');
$('#deliveryDiv').show();
$('#sizeguideDiv, #returnsDiv, #currencyDiv').hide();
return false;
});
$('a#currencyLink').click(function() {
$(this).addClass('selected');
$('a#returnsLink, a#sizeguideLink, a#deliveryLink').removeClass('selected');
$('#currencyDiv').show();
$('#sizeguideDiv, #returnsDiv, #deliveryDiv').hide();
return false;
});
});
html代码
<div class="productPagenav">
<a id="sizeguideLink" href="#">Size Guide</a>
<a id="returnsLink" href="#">Returns</a>
<a id="deliveryLink" href="#">Delivery</a>
<a id="currencyLink" href="#">Currencies</a>
<div id="sizeguideDiv">
</div>
<div id="returnsDiv">
</div>
<div id="deliveryDiv">
</div>
<div id="currencyDiv">
</div>
</div>
looking to simplify the jquery function bellow, which hides and shows a number divs with click(), could use next(), but not familiar with the way to implement it.
$(function() {
$('a#deliveryLink').addClass('selected');
$('#sizeguideDiv, #returnsDiv, #currencyDiv').hide();
$('a#sizeguideLink').click(function() {
$(this).addClass('selected');
$('a#deliveryLink, a#returnsLink, a#currencyLink').removeClass('selected');
$('#sizeguideDiv').show();
$('#deliveryDiv, #returnsDiv, #currencyDiv').hide();
return false;
});
$('a#returnsLink').click(function() {
$(this).addClass('selected');
$('a#deliveryLink, a#sizeguideLink, a#currencyLink').removeClass('selected');
$('#returnsDiv').show();
$('#sizeguideDiv, #deliveryDiv, #currencyDiv').hide();
return false;
});
$('a#deliveryLink').click(function() {
$(this).addClass('selected');
$('a#returnsLink, a#sizeguideLink, a#currencyLink').removeClass('selected');
$('#deliveryDiv').show();
$('#sizeguideDiv, #returnsDiv, #currencyDiv').hide();
return false;
});
$('a#currencyLink').click(function() {
$(this).addClass('selected');
$('a#returnsLink, a#sizeguideLink, a#deliveryLink').removeClass('selected');
$('#currencyDiv').show();
$('#sizeguideDiv, #returnsDiv, #deliveryDiv').hide();
return false;
});
});
html code
<div class="productPagenav">
<a id="sizeguideLink" href="#">Size Guide</a>
<a id="returnsLink" href="#">Returns</a>
<a id="deliveryLink" href="#">Delivery</a>
<a id="currencyLink" href="#">Currencies</a>
<div id="sizeguideDiv">
</div>
<div id="returnsDiv">
</div>
<div id="deliveryDiv">
</div>
<div id="currencyDiv">
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我不知道你到底想解决什么问题,但看起来你正在尝试类似手风琴的东西......
也许这个链接可以帮助你http://docs.jquery.com/UI/Accordion
Html
JQuery/Javascript
hth
i don't know exactly what you are trying to solve, but it looks like you are trying something like an accordion...
maybe this link could help you http://docs.jquery.com/UI/Accordion
Html
JQuery/Javascript
hth
为所有锚点添加类并
add class to all anchors and
HTML:
JavaScript:
现场演示: http://jsfiddle.net/simevidas/34KWj/
没有锚点就更简单了: http://jsfiddle.net/simevidas/34KWj/ 1/
HTML:
JavaScript:
Live demo: http://jsfiddle.net/simevidas/34KWj/
It is even simpler without the anchors: http://jsfiddle.net/simevidas/34KWj/1/
试试这个:
像这样应用:
showDiv('deliveryDiv');
编辑:细化,只需向每个与 ID 同名的链接添加一个“rel”属性,检查我的示例:
http://jsfiddle.net/jackJoe/QL3Y9/
这是代码:
try this:
apply like this:
showDiv('deliveryDiv');
EDIT: Refinement, just add a "rel" attribute to every link with the same name as the ID, check my example:
http://jsfiddle.net/jackJoe/QL3Y9/
here's the code:
我得到它非常紧凑 http://jsfiddle.net/grFQu/
html:
css:
js:
I got it pretty compact http://jsfiddle.net/grFQu/
html:
css:
js:
下面是一个使用 jQuery 数据将菜单配置到某个 div 的示例。
HTML:
CSS:
JavaScript:
我喜欢这个的原因是 JavaScript / CSS 是通用的,并且在添加新链接时保持不变。您只需要配置 jQuery 'data-id' 属性。这是一个 jsFiddle
http://jsfiddle.net/hqcVZ/2/
希望有所帮助。
鲍勃
Here is an example of using jQuery data to configure the menu to a certain div.
HTML:
CSS:
JavaScript:
The reason I like this is that the JavaScript / CSS is generic and remains the same as you add new links. You only need to configure the jQuery 'data-id' attribute. Here is a jsFiddle
http://jsfiddle.net/hqcVZ/2/
Hope that helps.
Bob