jQuery - addClass 或removeClass - 是否有附加类?
我将 css 悬停在选项卡的图像上,当我单击图像 HOW 时,我试图将类从 .how 更改为 .how_on。
我的标签是
如何|什么 |何时 |世界卫生组织 |为什么
我为每个 (.how, .how_on)、(.what, .what_on) 等都有类...
我可以使用 click(function(){}); 让 jQuery 将 _on 添加到原始类名中吗? ?
HTML:
<div id="tab_container">
<ul class="tabs">
<li><a class="how_on" href="#how">How</a></li>
<li><a class="why" href="#why">Why</a></li>
<li><a class="what" href="#what">What</a></li>
<li><a class="who" href="#who">Who</a></li>
<li><a class="when" href="#when">When</a></li>
</ul>
<p><img src="images/tab_top.jpg" width="864px" height="6px" alt="" border="0" /></p>
</div>
<div class="tab_body">
<!-- HOW -->
<div id="how" class="tab">
<strong>HOW IT WORKS:</strong>
</div>
JQUERY:
<script type="text/javascript">
jQuery(document).ready(function(){
//if this is not the first tab, hide it
jQuery(".tab:not(:first)").hide();
//to fix u know who
jQuery(".tab:first").show();
//when we click one of the tabs
jQuery(".tabs a").click(function(){
//get the ID of the element we need to show
stringref = jQuery(this).attr("href").split('#')[1];
// adjust css on tabs to show active tab
//hide the tabs that doesn't match the ID
jQuery('.tab:not(#'+stringref+')').hide();
//fix
if (jQuery.browser.msie && jQuery.browser.version.substr(0,3) == "6.0") {
jQuery('.tab#' + stringref).show();
}
else
//display our tab fading it in
jQuery('.tab#' + stringref).fadeIn();
//stay with me
return false;
});
});
</script>
CSS:
.tabs
{
width: 683px;
height: 29px;
padding: 0;
margin: 0;
display: inline;
float: left;
overflow: hidden;
list-style-type: none;
}
.tabs li
{
margin: 0;
padding: 0;
display: inline;
float: left;
}
.tabs a { background-position: 0 -58px;}
.tabs .on a { background-position: 0 -29px;}
.how,
a.how:link,
a.how:visited
{
float: left;
display: inline;
width: 135px;
height: 29px;
margin: 0;
text-decoration: none;
text-indent: -99999px;
overflow: hidden;
background-image: url("../images/how_tab.jpg");
}
I have css hover over images for my tabs and I'm trying to get the class to change from .how to .how_on when I click on the image HOW.
My tabs are
HOW | WHAT | WHEN | WHO | WHY
I have classes for each (.how, .how_on), (.what, .what_on), etc...
Can I make jQuery add _on to the original class name using click(function(){}); ?
HTML:
<div id="tab_container">
<ul class="tabs">
<li><a class="how_on" href="#how">How</a></li>
<li><a class="why" href="#why">Why</a></li>
<li><a class="what" href="#what">What</a></li>
<li><a class="who" href="#who">Who</a></li>
<li><a class="when" href="#when">When</a></li>
</ul>
<p><img src="images/tab_top.jpg" width="864px" height="6px" alt="" border="0" /></p>
</div>
<div class="tab_body">
<!-- HOW -->
<div id="how" class="tab">
<strong>HOW IT WORKS:</strong>
</div>
JQUERY:
<script type="text/javascript">
jQuery(document).ready(function(){
//if this is not the first tab, hide it
jQuery(".tab:not(:first)").hide();
//to fix u know who
jQuery(".tab:first").show();
//when we click one of the tabs
jQuery(".tabs a").click(function(){
//get the ID of the element we need to show
stringref = jQuery(this).attr("href").split('#')[1];
// adjust css on tabs to show active tab
//hide the tabs that doesn't match the ID
jQuery('.tab:not(#'+stringref+')').hide();
//fix
if (jQuery.browser.msie && jQuery.browser.version.substr(0,3) == "6.0") {
jQuery('.tab#' + stringref).show();
}
else
//display our tab fading it in
jQuery('.tab#' + stringref).fadeIn();
//stay with me
return false;
});
});
</script>
CSS:
.tabs
{
width: 683px;
height: 29px;
padding: 0;
margin: 0;
display: inline;
float: left;
overflow: hidden;
list-style-type: none;
}
.tabs li
{
margin: 0;
padding: 0;
display: inline;
float: left;
}
.tabs a { background-position: 0 -58px;}
.tabs .on a { background-position: 0 -29px;}
.how,
a.how:link,
a.how:visited
{
float: left;
display: inline;
width: 135px;
height: 29px;
margin: 0;
text-decoration: none;
text-indent: -99999px;
overflow: hidden;
background-image: url("../images/how_tab.jpg");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
更新:
尝试一下:
您正在使用类名创建唯一 ID。它将完成您正在尝试做的同样的事情。这是选择“原因”的示例。
当然,最简单的方法是添加 .how_on 类并拥有这两个类。
这没有什么问题,您可以轻松地覆盖样式或定位元素。
如果您想确保它覆盖它,请增加特异性。
UPDATED:
Try this:
You're using the Classname to create a Unique ID. It will accomplish the same thing that you're trying to do. Here's a sample with "why" selected.
Of course, THE SIMPLEST WAY is to add the .how_on class and have both classes.
Nothing wrong with that and you can easily override styles or target the element.
If you want to make sure it overrides it, increase the specificity.
当您使用
addClass
时,它会添加/追加一个类,除非您先使用removeClass
删除一些类,所以您可以这样做:When you use
addClass
, it adds/appends a class unless you remove some first usingremoveClass
, so you do like this:我希望 jQuery 确实有这个功能,但据我所知它没有。您始终可以使用
removeClass
删除一个版本,并使用addClass
添加另一个版本。或者,您可以编写自己的小插件。看起来是一件有用的事情;即使 jQuery UI 也必须处理修改它的所有类(“ui-state-default”、“ui-state-hover”等),如果它可以调用 API 来更新类主干“ui”,那就更容易了-state-”带有选定的后缀。因此,在您的情况下执行此操作的简单 jQuery 方法如下:
当您希望“how”选项卡切换到类“how_on”时,您可以调用
turnOn("how")
,以及turnOff("how")
使其从“how_on”转到“how”。I wish jQuery did have that, but as far as I can tell it doesn't. You can always use
removeClass
to get rid of one version andaddClass
to add the other. Alternatively, you could cook up your own little plugin. Seems like a useful thing to do; even jQuery UI has to deal with munging all its classes ("ui-state-default", "ui-state-hover", etc) and it'd be easier if it could just call an API to update the class stem "ui-state-" with a chosen suffix.Thus, the simple jQuery way to do it in your case would be something like:
You'd then call
turnOn("how")
when you want the "how" tab to switch to class "how_on", andturnOff("how")
to make it go from "how_on" to "how".addClass 采用匿名函数,您可以添加逻辑以在此处附加类,
例如
addClass takes an anonymous function and you can add the logic to append the class here
like
你总是可以这样做:
这将添加所需的后缀。
更好的方法是将“activation”类添加到包含“li”的类中。
你的CSS应该是这样的:
你可以为你的标签定义一个一般行为链接这个
you can always do:
which will add the desired suffix.
even better will be to add the "activation" class to the containing 'li'.
and your css should look like this:
and you can define a general behavior for your tabs link this
您可以用此完全替换您的 jQuery 部分;
这必须按预期工作!
JAVASCRIPT 部分
HTML 部分
希望这有帮助!
问候
You Can Completely Replace your jQuery Part with this;
This must work as expected!
JAVASCRIPT PART
HTML PART
Hope this help!
Regards
有几种方法。
_on
附加到字符串并添加新类。*_on
类即可修改原始类样式。然后您所要做的就是删除*_on
类以恢复到旧样式。There are a few approaches.
_on
to the string and add the new class.*_on
classes to modify the original classes styles. Then all you have to do is remove the*_on
class to revert to the old style.为什么不将
on
类添加到相关元素中,并将 CSS 规则从.how 和 .how_on
更改为.how 和 .how。上?
然后,在
.how.on
中,您可以覆盖.how
中您想要更改的任何设置。我认为您不需要按照您的意愿弯曲 JavaScript/jQuery,而是使用 CSS 特异性来实现它的目的。
Why don't you just add the class
on
to the elements in question and change your CSS rules from.how and .how_on
to.how and .how.on
?Then, in your
.how.on
you can just override anything set in the.how
that you want to change.I don't think you need to bend JavaScript/jQuery to your will, rather use CSS specificity for what it was meant for.