我正在使用以下代码在 3 个不同的 div 之间切换..
检查工作 DEMO .
我试图找到一种方法来将不同的背景颜色应用到当前选定的菜单选项卡(包括页面加载的默认选项卡 - “每周”),但没有成功
不可能给一个类添加一个类吧?
有什么想法可以用 jQuery 做到这一点吗?
这是我的代码...
jQuery :
jQuery('.viewSchedule').click(function () {
var index = $(this).index(),
newTarget = jQuery('.targetSched').eq(index);
console.log(index+newTarget)
jQuery('.targetSched').not(newTarget).fadeOut('fast')
newTarget.delay('fast').fadeIn('fast')
return false;
})
CSS:
.viewSchedule {}
.viewBTN {display:block;width:auto;height:auto;padding:5px 10px 5px 10px;background:#ccc;color:#242424;cursor:pointer;border-radius:5px;float:left;margin-left:10px;font-family:dina,Arial, Helvetica, sans-serif;font-size:16px;text-align:center;}
.viewBTN:hover {background:#FFF;color:#333;}
.targetSched {display: none}
.targetSched.first {display: block}
HTML:
<a class="viewSchedule" target="1"><span class="viewBTN">WEEKLY</span></a>
<a class="viewSchedule" target="2"><span class="viewBTN">DAILY</span></a>
<a class="viewSchedule" target="3"><span class="viewBTN">LIST</span></a>
<br /><br /><br /><br />
<div id="sh-week" class="targetSched first">WEEKLY CONTENT</div>
<div id="sh-daily" class="targetSched">DAILY CONTENT</div>
<div id="sh-list" class="targetSched">LIST CONTENT</div>
最佳答案
jQuery:
jQuery('.viewSchedule').click(function () {
$('.viewBTN').removeClass('selected');
$(this).find('.viewBTN').addClass('selected');
var index = $(this).index(),
newTarget = jQuery('.targetSched').eq(index);
jQuery('.targetSched').not(newTarget).fadeOut('fast')
newTarget.delay('fast').fadeIn('fast')
return false;
})
CSS:
.selected {background: blue}
关于jquery - 将类添加到当前菜单选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24394040/