Domanda

Ho il mouse CSS sulle immagini per le mie schede e sto cercando di far cambiare la classe da. Come a .how_on quando faccio clic sull'immagine come.

Le mie schede sono

Come | Cosa | Quando | Chi | PERCHÉ

Ho classi per ciascuno (.how, .how_on), (.what, .what_on), ecc ...

Posso fare jQuery ad aggiungere _on al nome della classe originale usando clic (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");
}
È stato utile?

Soluzione

Aggiornato:

Prova questo:

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

Stai usando il nome di classe per creare un ID univoco. Realizzerà la stessa cosa che stai cercando di fare. Ecco un campione con "Why" selezionato.

 <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>

Certo, Nel modo più semplice è aggiungere la classe .how_on e avere entrambe le classi.

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

Niente di sbagliato in questo e puoi facilmente sovrascrivere gli stili o indirizzare l'elemento.

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

Se vuoi assicurarti che lo sovrascrive, aumenta la specificità.

Altri suggerimenti

Quando usi addClass, aggiunge/aggiunge una classe a meno che non si rimuova un po 'di prima utilizzando removeClass, quindi ti piace questo:

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

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

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

Vorrei che JQuery ce l'avesse, ma per quanto posso dire no. Puoi sempre usare removeClass Per sbarazzarsi di una versione e addClass Per aggiungere l'altro. In alternativa, potresti cucinare il tuo piccolo plugin. Sembra una cosa utile da fare; Anche l'interfaccia utente di JQuery deve affrontare con tutte le sue classi ("Ui-State-Default", "Ui-State-Hover", ecc.) E sarebbe più facile se potesse semplicemente chiamare un'API per aggiornare l'interfaccia utente della classe " -stato- "Con un suffisso scelto.

Pertanto, il semplice modo jQuery per farlo nel tuo caso sarebbe qualcosa di simile:

// 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);
}

Quindi chiameresti turnOn("how") Quando si desidera che la scheda "How" passino alla classe "How_on", e turnOff("how") Per farlo passare da "How_on" a "How".

.addClass( function(index, class) )

AddClass prende una funzione anonima e puoi aggiungere la logica per aggiungere la classe qui

piace

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

Puoi sempre fare:

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

che aggiungerà il suffisso desiderato.

Ancora meglio sarà aggiungere la classe "Attivazione" alla "li" contenente.

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

E il tuo CSS dovrebbe apparire così:

/* 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;
}

E puoi definire un comportamento generale per le tue schede collegano questo

.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 */ }

Puoi Sostituisci completamente la parte jQuery con questo;

Questo deve funzionare come previsto!

Parte 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();
});

Parte 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>

Spero questo aiuto!

Saluti

Ci sono alcuni approcci.

  1. Rimuovi la classe, aggiungi _on alla stringa e aggiungi la nuova classe.
  2. Basta progettare il tuo *_on Classi per modificare gli stili delle classi originali. Allora tutto ciò che devi fare è rimuovere il *_on classe per tornare al vecchio stile.

Perché non aggiungi la lezione on agli elementi in questione e modifica le regole CSS da .how and .how_on a .how and .how.on?

Quindi, nel tuo .how.on Puoi semplicemente sovrascrivere qualsiasi cosa impostata in .how che vuoi cambiare.

Non credo che tu debba piegare Javascript/jQuery alla tua volontà, piuttosto usare la specificità CSS per ciò per cui era destinato.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top