jQuery 显示/隐藏多个 div
请帮助我在 jQuery 中显示/隐藏多个 div。我有一个 PHP 代码,可以生成多个选项卡和多个框,其 id 几乎相同。这是生成的 HTML 行:
<ul id="tab-country">
<li><a id="africa" rel="1" href="#">africa</a></li>
<li><a id="europe" rel="1" href="#">europe</a></li>
<li><a id="asia" rel="1" href="#">asia</a></li>
</ul>
<div id="country-glass-1-africa" style="display:none;">Africa Glass 1</div>
<div id="country-glass-1-europe" style="display:none;">Europe Glass 1</div>
<div id="country-glass-1-asia" style="display:none;">Asia Glass 1</div>
<div id="country-spoon-1-africa" style="display:none;">Africa Spoon 1</div>
<div id="country-spoon-1-europe" style="display:none;">Europe Spoon 1</div>
<div id="country-spoon-1-asia" style="display:none;">Asia Spoon 1</div>
<ul id="tab-country">
<li><a id="africa" rel="2" href="#">africa</a></li>
<li><a id="europe" rel="2" href="#">europe</a></li>
<li><a id="asia" rel="2" href="#">asia</a></li>
</ul>
<div id="country-glass-2-africa" style="display:none;">Africa Glass 2</div>
<div id="country-glass-2-europe" style="display:none;">Europe Glass 2</div>
<div id="country-glass-2-asia" style="display:none;">Asia Glass 2</div>
<div id="country-spoon-2-africa" style="display:none;">Africa Spoon 2</div>
<div id="country-spoon-2-europe" style="display:none;">Europe Spoon 2</div>
<div id="country-spoon-2-asia" style="display:none;">Asia Spoon 2</div>
在页面末尾,这是 jQuery 行:
$(function(){
$("[id$='-africa']").toggle();
$("#tab-country li a").click(function(event){
var country = $(this).attr('id');
var itemid = $(this).attr('rel');
/* the following 2 lines are not working - i want to hide them if they're shown */
$("[id^='country-glass-']"+itemid+"-").css('display','block').toggle();
$("[id^='country-spoon-']"+itemid+"-").css('display','block').toggle();
/* the following works as it is supposed to be */
$("#country-glass-"+itemid+"-"+country).toggle();
$("#country-spoon-"+itemid+"-"+country).toggle();
event.preventDefault();
});
});
单击其中一个选项卡国家项目时,请帮助我隐藏显示的 div。 无论如何,感谢您的关注。
Please help me in showing/hiding multiple divs in jQuery. I have a PHP code that produce multiple tabs and multiple boxes, with ids that nearly identical. Here is the produced HTML lines:
<ul id="tab-country">
<li><a id="africa" rel="1" href="#">africa</a></li>
<li><a id="europe" rel="1" href="#">europe</a></li>
<li><a id="asia" rel="1" href="#">asia</a></li>
</ul>
<div id="country-glass-1-africa" style="display:none;">Africa Glass 1</div>
<div id="country-glass-1-europe" style="display:none;">Europe Glass 1</div>
<div id="country-glass-1-asia" style="display:none;">Asia Glass 1</div>
<div id="country-spoon-1-africa" style="display:none;">Africa Spoon 1</div>
<div id="country-spoon-1-europe" style="display:none;">Europe Spoon 1</div>
<div id="country-spoon-1-asia" style="display:none;">Asia Spoon 1</div>
<ul id="tab-country">
<li><a id="africa" rel="2" href="#">africa</a></li>
<li><a id="europe" rel="2" href="#">europe</a></li>
<li><a id="asia" rel="2" href="#">asia</a></li>
</ul>
<div id="country-glass-2-africa" style="display:none;">Africa Glass 2</div>
<div id="country-glass-2-europe" style="display:none;">Europe Glass 2</div>
<div id="country-glass-2-asia" style="display:none;">Asia Glass 2</div>
<div id="country-spoon-2-africa" style="display:none;">Africa Spoon 2</div>
<div id="country-spoon-2-europe" style="display:none;">Europe Spoon 2</div>
<div id="country-spoon-2-asia" style="display:none;">Asia Spoon 2</div>
And at the end of the page, here is the jQuery lines:
$(function(){
$("[id$='-africa']").toggle();
$("#tab-country li a").click(function(event){
var country = $(this).attr('id');
var itemid = $(this).attr('rel');
/* the following 2 lines are not working - i want to hide them if they're shown */
$("[id^='country-glass-']"+itemid+"-").css('display','block').toggle();
$("[id^='country-spoon-']"+itemid+"-").css('display','block').toggle();
/* the following works as it is supposed to be */
$("#country-glass-"+itemid+"-"+country).toggle();
$("#country-spoon-"+itemid+"-"+country).toggle();
event.preventDefault();
});
});
Please help me in hiding the shown divs, when one of the tab-country item is clicked.
Anyway, thank you for your attention.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 2 行不起作用的原因是您的
[id=<>]
选择器之外有 itemid。您还可以执行以下操作:
$('selector:visible').hide();
仅隐藏可见内容,而不是将显示设置为阻止然后切换。The reason your 2 lines aren't working is because you have itemid outside your
[id=<>]
selector.You can also do something like this:
$('selector:visible').hide();
to only hide visible content, rather than setting display to block and then toggle.我认为你想要 [] 内的 itemid,例如
$("[id^='country-glass-' + itemid + '-']").css...
I think you want the itemid inside the [], like
$("[id^='country-glass-' + itemid + '-']").css...