jQuery - addClass 或removeClass - 是否有附加类?

发布于 2024-08-24 09:15:40 字数 2502 浏览 7 评论 0原文

我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

还给你自由 2024-08-31 09:15:40

更新:

尝试一下:

$('.tabs a').click(function() {
    $('.tabs a').removeAttr('id'); // remove all ids
    $(this).attr('id', this.className + "_on") // create how_on as this id.
});

您正在使用类名创建唯一 ID。它将完成您正在尝试做的同样的事情。这是选择“原因”的示例。

 <ul class="tabs">
    <li><a class="how" href="#how">How</a></li>
    <li><a id='why_on' 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>

当然,最简单的方法是添加 .how_on 类并拥有这两个类。

<div class='how how_on'>some stuff</div>

这没有什么问题,您可以轻松地覆盖样式或定位元素。

.how { color: #000; }
div.how_on { color: #F00; } /* increased specificity */

如果您想确保它覆盖它,请增加特异性。

UPDATED:

Try this:

$('.tabs a').click(function() {
    $('.tabs a').removeAttr('id'); // remove all ids
    $(this).attr('id', this.className + "_on") // create how_on as this id.
});

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.

 <ul class="tabs">
    <li><a class="how" href="#how">How</a></li>
    <li><a id='why_on' 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>

Of course, THE SIMPLEST WAY is to add the .how_on class and have both classes.

<div class='how how_on'>some stuff</div>

Nothing wrong with that and you can easily override styles or target the element.

.how { color: #000; }
div.how_on { color: #F00; } /* increased specificity */

If you want to make sure it overrides it, increase the specificity.

维持三分热 2024-08-31 09:15:40

当您使用 addClass 时,它会添加/追加一个类,除非您先使用 removeClass 删除一些类,所以您可以这样做:

$('.how_on').mouseover(function(){
    $(this).removeClass('class_name').addClass('class_name');
});

$('.why').mouseover(function(){
    $(this).removeClass('class_name').addClass('class_name');
});

// and so on.........

When you use addClass, it adds/appends a class unless you remove some first using removeClass, so you do like this:

$('.how_on').mouseover(function(){
    $(this).removeClass('class_name').addClass('class_name');
});

$('.why').mouseover(function(){
    $(this).removeClass('class_name').addClass('class_name');
});

// and so on.........
债姬 2024-08-31 09:15:40

我希望 jQuery 确实有这个功能,但据我所知它没有。您始终可以使用 removeClass 删除一个版本,并使用 addClass 添加另一个版本。或者,您可以编写自己的小插件。看起来是一件有用的事情;即使 jQuery UI 也必须处理修改它的所有类(“ui-state-default”、“ui-state-hover”等),如果它可以调用 API 来更新类主干“ui”,那就更容易了-state-”带有选定的后缀。

因此,在您的情况下执行此操作的简单 jQuery 方法如下:

// to go from "foo" to "foo_on":
function turnOn(which) {
  $('.tabs').find('a.' + which)
    .removeClass(which)
    .addClass(which + '_on');
};

// to go from "foo_on" to "foo"
function turnOff(which) {
  $('.tabs').find('a.' + which + '_on')
    .removeClass(which + '_on')
    .addClass(which);
}

当您希望“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 and addClass 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:

// to go from "foo" to "foo_on":
function turnOn(which) {
  $('.tabs').find('a.' + which)
    .removeClass(which)
    .addClass(which + '_on');
};

// to go from "foo_on" to "foo"
function turnOff(which) {
  $('.tabs').find('a.' + which + '_on')
    .removeClass(which + '_on')
    .addClass(which);
}

You'd then call turnOn("how") when you want the "how" tab to switch to class "how_on", and turnOff("how") to make it go from "how_on" to "how".

不打扰别人 2024-08-31 09:15:40
.addClass( function(index, class) )

addClass 采用匿名函数,您可以添加逻辑以在此处附加类,

例如

$('ELEMENT SELECTION').addClass(function() {
  return $(this).attr("class") + 'CLASS TO BE ADDED';
});
.addClass( function(index, class) )

addClass takes an anonymous function and you can add the logic to append the class here

like

$('ELEMENT SELECTION').addClass(function() {
  return $(this).attr("class") + 'CLASS TO BE ADDED';
});
葬花如无物 2024-08-31 09:15:40

你总是可以这样做:

jQuery(".tabs a").click(function(){
    this.className += '_on';
}

这将添加所需的后缀。

更好的方法是将“activation”类添加到包含“li”的类中。

jQuery(".tabs a").click(function(){
    $(this).parent().toggleClass('on');
}

你的CSS应该是这样的:

/* here i removed the 'a.how_on' entry */
.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;  
}

a.how:visited, a.how:link, a.how:hover
{
    background-image: url("../images/how_tab.jpg");
    background-position: 0 -58px;
}

/* this will cause the link to behave different when its parent is of class on */
.on a.how
{
    background-image: url("../images/how_tab.jpg");
    background-position: 0 -29px;
}

你可以为你的标签定义一个一般行为链接这个

.tabs a { /* style */ }
.tabs a:link { /* link style */ }
.tabs a:hover { /* hover style */ }

.tabs .on a { /* activated style */ }
.tabs .on a:link { /* activated link style */ }
.tabs .on a:hover { /* activated hover style */ }

you can always do:

jQuery(".tabs a").click(function(){
    this.className += '_on';
}

which will add the desired suffix.

even better will be to add the "activation" class to the containing 'li'.

jQuery(".tabs a").click(function(){
    $(this).parent().toggleClass('on');
}

and your css should look like this:

/* here i removed the 'a.how_on' entry */
.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;  
}

a.how:visited, a.how:link, a.how:hover
{
    background-image: url("../images/how_tab.jpg");
    background-position: 0 -58px;
}

/* this will cause the link to behave different when its parent is of class on */
.on a.how
{
    background-image: url("../images/how_tab.jpg");
    background-position: 0 -29px;
}

and you can define a general behavior for your tabs link this

.tabs a { /* style */ }
.tabs a:link { /* link style */ }
.tabs a:hover { /* hover style */ }

.tabs .on a { /* activated style */ }
.tabs .on a:link { /* activated link style */ }
.tabs .on a:hover { /* activated hover style */ }
夜深人未静 2024-08-31 09:15:40

您可以用此完全替换您的 jQuery 部分

这必须按预期工作!

JAVASCRIPT 部分

$(function() {

    var tabs = $('div.tab_body > div.tab');
    tabs.hide().filter(':first').show();

    $('div#tab_container ul.tabs a').click(function() {
        tabs.hide();
        var myhash = this.hash.split('#')[1];

        tabs.filter(this.hash).show();

        $('a:not(.' + myhash + '_on)').each(function() {
            var h = this.hash.split('#')[1];
            $(this).removeClass(h + '_on').addClass(h);
        });
        $(this).removeClass(myhash).addClass(myhash + '_on');

        return false;
    }).filter(':first').click();
});

HTML 部分

<div id="tab_container">
 <ul class="tabs">
    <li><a class="how" 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>            
</div>

<div class="tab_body">
    <div id="how" class="tab">
        <strong>how</strong>
    </div>
    <div id="why" class="tab">
        <strong>why:</strong>
    </div>
    <div id="what" class="tab">
        <strong>what</strong>
    </div>
    <div id="who" class="tab">
        <strong>who</strong>
    </div>
    <div id="when" class="tab">
     <strong>when</strong>
    </div>
  </div>

希望这有帮助!

问候

You Can Completely Replace your jQuery Part with this;

This must work as expected!

JAVASCRIPT PART

$(function() {

    var tabs = $('div.tab_body > div.tab');
    tabs.hide().filter(':first').show();

    $('div#tab_container ul.tabs a').click(function() {
        tabs.hide();
        var myhash = this.hash.split('#')[1];

        tabs.filter(this.hash).show();

        $('a:not(.' + myhash + '_on)').each(function() {
            var h = this.hash.split('#')[1];
            $(this).removeClass(h + '_on').addClass(h);
        });
        $(this).removeClass(myhash).addClass(myhash + '_on');

        return false;
    }).filter(':first').click();
});

HTML PART

<div id="tab_container">
 <ul class="tabs">
    <li><a class="how" 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>            
</div>

<div class="tab_body">
    <div id="how" class="tab">
        <strong>how</strong>
    </div>
    <div id="why" class="tab">
        <strong>why:</strong>
    </div>
    <div id="what" class="tab">
        <strong>what</strong>
    </div>
    <div id="who" class="tab">
        <strong>who</strong>
    </div>
    <div id="when" class="tab">
     <strong>when</strong>
    </div>
  </div>

Hope this help!

Regards

柳絮泡泡 2024-08-31 09:15:40

有几种方法。

  1. 删除该类,将 _on 附加到字符串并添加新类。
  2. 只需设计您的 *_on 类即可修改原始类样式。然后您所要做的就是删除 *_on 类以恢复到旧样式。

There are a few approaches.

  1. Remove the class, append _on to the string and add the new class.
  2. Simply design your *_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.
司马昭之心 2024-08-31 09:15:40

为什么不将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文