Javascript 需要帮助简化代码吗
好的,我有一个关于Drupal安装的页面,它有多个div。我编写了一个.js来测试这些div中是否有任何信息Javascript 需要帮助简化代码吗,javascript,jquery,show-hide,html,Javascript,Jquery,Show Hide,Html,好的,我有一个关于Drupal安装的页面,它有多个div。我编写了一个.js来测试这些div中是否有任何信息 if ($('.accred').length) { $('#accred').show(); } else{ $('#accred').hide(); } if ($('.admin-req').length) { $('#admis').show(); } else{ $('#admis').hide(); } if ($('.care
if ($('.accred').length) {
$('#accred').show();
}
else{
$('#accred').hide();
}
if ($('.admin-req').length) {
$('#admis').show();
}
else{
$('#admis').hide();
}
if ($('.career-opp').length) {
$('#career').show();
}
else{
$('#career').hide();
}
if ($('.co-op-diploma').length) {
$('#co_op').show();
}
else{
$('#co_op').hide();
}
if ($('.prog-out').length) {
$('#outcomes').show();
}
else{
$('#outcomes').hide();
}
if ($('.prog-struc').length) {
$('#struc').show();
}
else{
$('#struc').hide();
}
if ($('.testimonials').length) {
$('#testimon').show();
}
else{
$('#testimon').hide();
}
if ($('.transfer').length) {
$('#transfer').show();
}
else{
$('#transfer').hide();
}
if ($('.tuition').length) {
$('#tuition').show();
}
else{
$('#tuition').hide();
}
要隐藏或显示允许您单击并显示关于每个div的更多信息的链接,因为默认情况下除了一个div之外的每个div都是隐藏的:$(function(){
$('#descrip').click(function(){
$('.prog-descrip').show();
$('.accred').hide();
$('.admin-req').hide();
$('.career-opp').hide();
$('.co-op-diploma').hide();
$('.prog-out').hide();
$('.prog-struc').hide();
$('.testimonials').hide();
$('.transfer').hide();
$('.tuition').hide();
});
$('#accred').click(function(){
$('.prog-descrip').hide();
$('.accred').show();
$('.admin-req').hide();
$('.career-opp').hide();
$('.co-op-diploma').hide();
$('.prog-out').hide();
$('.prog-struc').hide();
$('.testimonials').hide();
$('.transfer').hide();
$('.tuition').hide();
});
$('#admis').click(function(){
$('.prog-descrip').hide();
$('.accred').hide();
$('.admin-req').show();
$('.career-opp').hide();
$('.co-op-diploma').hide();
$('.prog-out').hide();
$('.prog-struc').hide();
$('.testimonials').hide();
$('.transfer').hide();
$('.tuition').hide();
});
$('#career').click(function(){
$('.prog-descrip').hide();
$('.accred').hide();
$('.admin-req').hide();
$('.career-opp').show();
$('.co-op-diploma').hide();
$('.prog-out').hide();
$('.prog-struc').hide();
$('.testimonials').hide();
$('.transfer').hide();
$('.tuition').hide();
});
$('#co_op').click(function(){
$('.prog-descrip').hide();
$('.accred').hide();
$('.admin-req').hide();
$('.career-opp').hide();
$('.co-op-diploma').show();
$('.prog-out').hide();
$('.prog-struc').hide();
$('.testimonials').hide();
$('.transfer').hide();
$('.tuition').hide();
});
$('#outcomes').click(function(){
$('.prog-descrip').hide();
$('.accred').hide();
$('.admin-req').hide();
$('.career-opp').hide();
$('.co-op-diploma').hide();
$('.prog-out').show();
$('.prog-struc').hide();
$('.testimonials').hide();
$('.transfer').hide();
$('.tuition').hide();
});
$('#struc').click(function(){
$('.prog-descrip').hide();
$('.accred').hide();
$('.admin-req').hide();
$('.career-opp').hide();
$('.co-op-diploma').hide();
$('.prog-out').hide();
$('.prog-struc').show();
$('.testimonials').hide();
$('.transfer').hide();
$('.tuition').hide();
});
$('#testimon').click(function(){
$('.prog-descrip').hide();
$('.accred').hide();
$('.admin-req').hide();
$('.career-opp').hide();
$('.co-op-diploma').hide();
$('.prog-out').hide();
$('.prog-struc').hide();
$('.testimonials').show();
$('.transfer').hide();
$('.tuition').hide();
});
$('#transfer').click(function(){
$('.prog-descrip').hide();
$('.accred').hide();
$('.admin-req').hide();
$('.career-opp').hide();
$('.co-op-diploma').hide();
$('.prog-out').hide();
$('.prog-struc').hide();
$('.testimonials').hide();
$('.transfer').show();
$('.tuition').hide();
});
$('#tuition').click(function(){
$('.prog-descrip').hide();
$('.accred').hide();
$('.admin-req').hide();
$('.career-opp').hide();
$('.co-op-diploma').hide();
$('.prog-out').hide();
$('.prog-struc').hide();
$('.testimonials').hide();
$('.transfer').hide();
$('.tuition').show();
});
});
然而,客户不喜欢这种方式。他希望根据是否有更多div进行填充
所以,我的问题是:如何使用js或jquery填充所有的div以及显示/隐藏div的代码,基于它们动态单击的链接?我不是一个js或jquery的家伙,所以请原谅我的天真问题,如果它实际上是天真的。谢谢
编辑
请把我的问题重读给回答过的人听。动态填充信息。换句话说,客户端不需要硬编码的div。第二部分可以缩短为:
$('.accred, .admin-req, .career-opp, .co-op-diploma, .prog-out, .prog-struc,
.testimonials, .transfer, .tuition').hide();
//Show the one you want
更好的解决方案是,只需提供隐藏自己的类并使用.toggle()所需的所有类。好的,下面是我最后要做的。最后,我给每个链接分配了linkeddiv类,以及内容div名称的rel,如下所示:
<a href="#tuition" class="linkeddiv" rel="tuition">Tuition and Fees</a>
<div class="prog-descrip prog-detail" style="display: none; ">
完成后,我测试了他们点击的链接,并基于此,我隐藏或显示了基于rel链接的div:
$('.linkeddiv').each(function() {
contentdiv = $(this).attr('rel');
if ($('.' + contentdiv).length == 0) {
$(this).hide();
}
});
$('.linkeddiv').click(function() {
$('.prog-detail').hide();
contentdiv = $(this).attr('rel');
$('.' + contentdiv).show();
});
这正是我想要的。此外,它还添加了使用后退按钮的功能,而不是导致死链接(#)
如果您想使用jquery,还可以进行一些有趣的淡入淡出或滑动
希望这能帮助别人。是的,好主意。另一个类会很方便,然后只需要一个函数来完成这些操作,而不是为每个div生成相同的代码。这会很困难,因为它们都是单独显示的,而不是作为一个组显示的。听起来这个问题有几个方面,如果没有示例,很难在一篇文章中表达出来。你应该加入javascript聊天:我很乐意,但我没有聊天的名声。
$('.linkeddiv').each(function() {
contentdiv = $(this).attr('rel');
if ($('.' + contentdiv).length == 0) {
$(this).hide();
}
});
$('.linkeddiv').click(function() {
$('.prog-detail').hide();
contentdiv = $(this).attr('rel');
$('.' + contentdiv).show();
});