使用jQuery添加类,但要确保它使用类之后的所有标记
Adding class with jQuery but make sure it uses all the tags after the class
我不知道如何在标题中使用这个词,但假设你有这样的东西:
<script type="text/javascript">
$(document).ready(function(){
$('#tabp1').show();
$('#tabp2').hide();
$('#tabp3').hide();
$('#tab1').addClass("activeTab");
$('#tab1').css("activeTab");
})
function tab1(){
$('#tabp1').show();
$('#tabp2').hide();
$('#tabp3').hide();
$('#tab1').addClass("activeTab"); // add class activeTab to show active
$('#tab2').removeClass("activeTab"); // remove the class from the pre selected ones
$('#tab3').removeClass("activeTab"); // remove the class from the pre selected ones
}
function tab2(){
$('#tabp2').show();
$('#tabp1').hide();
$('#tabp3').hide();
$('#tab1').removeClass("activeTab"); // remove the class from the pre selected ones
$('#tab2').addClass("activeTab"); // add class activeTab to show active
$('#tab3').removeClass("activeTab"); // remove the class from the pre selected ones
}
function tab3(){
$('#tabp1').hide();
$('#tabp3').show();
$('#tabp2').hide();
$('#tab1').removeClass("activeTab"); // remove the class from the pre selected ones
$('#tab2').removeClass("activeTab"); // remove the class from the pre selected ones
$('#tab3').addClass("activeTab"); // add class activeTab to show active
}
</script>
和HTML
<div id="table-tabs">
<ul>
<li id="tab1" class="tab"><a onClick="tab1();">Accommodation</a></li>
<li id="tab2" class="tab"><a onClick="tab2();">Special Offers</a></li>
<li id="tab3" class="tab"><a onClick="tab3();">Photo Gallery</a></li>
</ul>
<div id="tabp1">Tab 1: nulla facilisi. Nam liber tempor cum soluta
nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus</div>
<div id="tabp2">Tab 2 content</div>
<div id="tabp3">This one here is tab 3, full of Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam
erat volutpat. Ut wisi enim ad minim veniam,
quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</div>
</div>
</div><!-- end table tabs -->
最后,CSS:
#content-left #mainLeftContent #table-tabs ul li.oldTab{
/* sets background and text color */
}
#content-left #mainLeftContent #table-tabs ul li.activeTab a{
color: white;
}
#content-left #mainLeftContent #table-tabs ul li.activeTab{
background-color: #015f01;
}
#content-left #mainLeftContent #table-tabs ul li.tab a{
color: black;
}
#content-left #mainLeftContent #table-tabs ul li.tab{
padding: 10px;
-webkit-border-top-left-radius: 10px;
-webkit-border-top-right-radius: 10px;
-moz-border-radius-topleft: 10px;
-moz-border-radius-topright: 10px;
border-top-left-radius: 10px;
border-top-right-radius: 10px;
color: white;
}
我需要javascript不仅选择:
#content-left #mainLeftContent #table-tabs ul li.activeTab
但也包括:
#content-left #mainLeftContent #table-tabs ul li.activeTab a
这应该是可能的,有什么想法吗?
只是为了澄清这里的问题:
javascript正确地应用了类activeTab,但是我需要上面的文本是白色的,所以当我选择一个(在活动选项卡之后)并将颜色更改为白色时,它不会应用它。
有趣的变化是,我改变了锚属性上的文本大小,它也改变了大小,所以我认为这是颜色的问题
这两个选择器同样精确,这意味着最后一个声明的选择器将使用
#content-left #mainLeftContent #table-tabs ul li.activeTab a{
color: white;
}
#content-left #mainLeftContent #table-tabs ul li.tab a{
color: black;
}
你可以通过使用默认颜色来修复它,该颜色在激活时被覆盖
/* white when active (now more precise because of li.activeTab */
#content-left #mainLeftContent #table-tabs ul li.activeTab a{
color: white;
}
/* default black */
#content-left #mainLeftContent #table-tabs ul li a{
color: black;
}
因此,假设您选择了一个"#table tabs ul-lia":
$("#table-tabs ul li a").click( function(e) {
e.preventDefault();
$(this).parent().toggleClass('tab activeTab'); //Remove class tab, add class activeTab
});
你应该改变,只是位置!对于颜色很重要,它会修复
#content-left #mainLeftContent #table-tabs ul li.activeTab a{
color: white !important;
}
检查这个jsfiddlehttp://jsfiddle.net/ENS8N/
在调整CSS后,它对我有利,你不必添加所有额外的CSS引用。只要保持简单,它对我来说非常有效。
<style type="text/css">
li.oldTab{
/* sets background and text color */
}
li.tab a{
color: black;
}
li.tab{
padding: 10px;
-webkit-border-top-left-radius: 10px;
-webkit-border-top-right-radius: 10px;
-moz-border-radius-topleft: 10px;
-moz-border-radius-topright: 10px;
border-top-left-radius: 10px;
border-top-right-radius: 10px;
color: white;
}
li.activeTab a{
color: white;
}
li.activeTab{
background: #999;
}
</style>
相关文章:
- 如何在侦听器之后添加可拖动功能
- AngularJS:在.post()之后添加事件侦听器
- jqgridnavgrid在搜索按钮和自定义按钮之后添加按钮
- 如何在wooccommerce脚本之后添加自定义java脚本
- 如果在transclusion之后添加,为什么指令下的动态元素具有错误的作用域
- 在使用jquery验证电子邮件之后添加一个类
- 如何使用Javascript或Jquery在X个
- 之后添加子菜单
- 在jQuery的显示和隐藏动画之后添加类
- 煎茶触摸 - 在组件之后添加/插入
- Qualtrics:在矩阵表中的文本框之前/之后添加不同的字符
- 如何在 select.options.add(new Option()) 之后添加 2 个属性
- 如何在 angularjs 中的简单获取请求 json 之后添加事件侦听器
- 在每组三个图像之后添加中断 jquery
- 使用 Jquery 在现有的 Hidden Div 之后添加 Div
- 在每 2 个
- 之后添加
- 之后添加
- 如何在表单中的每个输入/新行之后添加
标签 - 在showMessage()之后添加延迟
- 在state.go(ui路由器)之后添加功能
- 如何在#之后添加文本,但不能添加#
- 如何在replaceWith()之后添加事件