4

我将 css 悬停在标签的图像上,当我单击图像 HOW 时,我试图让课程从 .how 更改为 .how_on。

我的标签是

如何 | 什么 | 何时 | 世界卫生组织 | 为什么

我为每个(.how,.how_on),(.what,.what_on)等都有课程......

我可以让 jQuery 使用 click(function(){}) 将 _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>

查询:

<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");
}
4

8 回答 8

3

我希望 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);
}

然后turnOn("how"),当您希望“how”选项卡切换到“how_on”类并turnOff("how")使其从“how_on”变为“how”时,您会调用。

于 2010-03-11T17:06:41.597 回答
3

当你使用addClass时,它会添加/附加一个类,除非你先删除一些 using 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.........
于 2010-03-11T17:07:39.910 回答
2

更新:

试试这个:

$('.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 */

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

于 2010-03-11T17:26:37.143 回答
1
.addClass( function(index, class) )

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

$('ELEMENT SELECTION').addClass(function() {
  return $(this).attr("class") + 'CLASS TO BE ADDED';
});
于 2010-03-11T17:09:59.230 回答
1

你总是可以这样做:

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

这将添加所需的后缀。

更好的方法是将“激活”类添加到包含的“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 */ }
于 2010-03-11T17:43:04.160 回答
1

你可以用这个完全替换你的 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>

希望这有帮助!

问候

于 2010-03-11T20:48:00.407 回答
0

有几种方法。

  1. 删除类,附加_on到字符串并添加新类。
  2. 只需设计您的*_on类以修改原始类样式。然后您所要做的就是删除*_on该类以恢复到旧样式。
于 2010-03-11T17:08:38.087 回答
0

为什么不直接将类添加on到有问题的元素中,然后将 CSS 规则从 更改.how and .how_on.how and .how.on

然后,在您的中,.how.on您可以覆盖.how您想要更改的任何内容。

我不认为你需要让 JavaScript/jQuery 屈服于你的意愿,而是使用 CSS 的特殊性来实现它的目的。

于 2010-03-11T17:22:02.970 回答