jQuery 切换隐藏的 div 问题与多个单词名称
我有一个功能可以切换隐藏的 div 并且工作得很好。不幸的是,“品牌”部分刚刚更改为“品牌”。该代码由你们中的一位天才进行了慷慨的改进和动态编写,并使用锚点的内容名称来选择要显示/隐藏的 div。不幸的是我需要(太空)品牌。不,布埃诺!
有想法吗?谢谢你!
ps 如果您从“show_brand”锚点中取出 THE,则此功能有效。
HTML:
<div id="nav">
<div id="nav_left">
<a class="home" id="show_brand" title="BRAND">THE BRAND</a><br />
<a class="home" id="show_campaigns" title="CAMPAIGNS">CAMPAIGNS</a><br />
<a href="collection/" title="COLLECTION">COLLECTION</a><br />
<a class="home" id="show_inquiries" title="INQUIRIES">INQUIRIES</a>
</div>
<div id="campaigns">
<a href="campaigns/neo_balletto/" title="NEO BALLETTO">NEO BALLETTO</a><br />
<a href="campaigns/poetic_deco/" title="POETIC DECO">POETIC DECO</a>
</div>
</div>
<div class="current" id="brand">
<p>content</p>
</div>
<div id="inquiries">
<p>content</p>
</div>
JavaScript:
$('#brand, #campaigns, #inquiries').hide();
$('.home').click(function() {
var id = $(this).html().toLowerCase();
var $content = $('#' + id + ':not(:visible)');
if ($('.current').length === 0) {
showContent($content)
}
else {
$('.current').fadeOut(600, function() {
showContent($content)
});
}
$('.home').css('text-decoration', 'none');
$(this).css('text-decoration', 'underline');
});
function showContent(content) {
content.fadeIn(600);
$('.current').removeClass('current');
content.addClass('current');
}
$('.close').click(function() {
$('.current').fadeOut(600);
});
I have a function that toggles hidden divs and works beautifully at it. unfortunately, the "brand" section just got changed to "the brand." the code was graciously improved and written dynamically by one of you geniuses and uses the anchor's content name to select the div to show/hide. unfortunately i need THE (space) BRAND. No bueno!
Ideas? Thank you!
p.s. this function works if you take THE out of the "show_brand" anchor.
HTML:
<div id="nav">
<div id="nav_left">
<a class="home" id="show_brand" title="BRAND">THE BRAND</a><br />
<a class="home" id="show_campaigns" title="CAMPAIGNS">CAMPAIGNS</a><br />
<a href="collection/" title="COLLECTION">COLLECTION</a><br />
<a class="home" id="show_inquiries" title="INQUIRIES">INQUIRIES</a>
</div>
<div id="campaigns">
<a href="campaigns/neo_balletto/" title="NEO BALLETTO">NEO BALLETTO</a><br />
<a href="campaigns/poetic_deco/" title="POETIC DECO">POETIC DECO</a>
</div>
</div>
<div class="current" id="brand">
<p>content</p>
</div>
<div id="inquiries">
<p>content</p>
</div>
Javascript:
$('#brand, #campaigns, #inquiries').hide();
$('.home').click(function() {
var id = $(this).html().toLowerCase();
var $content = $('#' + id + ':not(:visible)');
if ($('.current').length === 0) {
showContent($content)
}
else {
$('.current').fadeOut(600, function() {
showContent($content)
});
}
$('.home').css('text-decoration', 'none');
$(this).css('text-decoration', 'underline');
});
function showContent(content) {
content.fadeIn(600);
$('.current').removeClass('current');
content.addClass('current');
}
$('.close').click(function() {
$('.current').fadeOut(600);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
那么只需将
var id = $(this).html().toLowerCase();
更改为
var id = $(this).attr("id").replace("show_" ,"").toLowerCase();
并且无论 div 的内容是什么,您的代码都会工作
检查 http://jsfiddle.net/3AnZw/1/ 进行演示
Well just change
var id = $(this).html().toLowerCase();
to
var id = $(this).attr("id").replace("show_","").toLowerCase();
And your code will work ,no matter what the content of the div is
Check http://jsfiddle.net/3AnZw/1/ for a demo
我能想到的两种方法:
1)使用标题属性导航到 DIV 并切换可见性。即:
2)将div的id从brand更改为the_brand并使用以下JS:
Two ways I can think of:
1) Use the title attribute to navigate to the DIV's and toggle the visibility. i.e:
2) Change the id of div from brand to the_brand and use the JS below:
如果你可以将id品牌更新为“the_brand”,那么你只需添加
即可将ID中的空格替换为下划线来选择它。如果这不是一个选项,并且您只想始终使用最后一个单词,请执行以下操作:
此正则表达式将替换最后一个空格之前的所有内容,留下最后一个单词(使用 THE THE BRAND 进行测试)
If you can update the id brand to "the_brand", then you can just add
That is, replace spaces with underscores in the ID to select it. If that's not an option and you just want to always use the last word, do this:
This regex will replace everything up to the last space leaving the last word (tested with THE THE BRAND)
尝试类似的东西:
try something like: