Using Part of a Class Name as a Variable for .addClass

Marcatectura

BACKGROUND: I have nine divs with classes .big1, .big2, .big3, and .small1, .small2, .small3. The divs aren't siblings or parents & children, or I'd be doing this with pure CSS.

I'm adding a class to .big1 when .small1 is hovered over, and .big2 when small2 is hovered over. My current approach (shown below) works but is inelegant. Though I'm pretty new to jQuery, I believe it's possible to shrink the code down to a single .addClass function, using variables to change the class names accordingly.

PROBLEM: I'm trying to set up my code so the number x on the end of a hovered .big*x* div is appended to the class name of the .small*x* div that follows, probably by creating a variable out of the numbers on the .big and .small classes. This is where I'm stuck. I've looked at using .split() to grab the number at the end of a .small and .replace() to put that number on the end of the .big, but I can't figure out how to set it up. Suggestions are appreciated!

jQuery:

$('.small1').hover(
  function(){
    $('.big1').addClass("itemHover");
  },function(){
    $('.big1').removeClass("itemHover");
});

$('.small2').hover(
  function(){
    $('.big2').addClass("itemHover");
  },function(){
    $('.big2').removeClass("itemHover");
});

$('.small3').hover(
  function(){
    $('.big3').addClass("itemHover");
  },function(){
    $('.big3').removeClass("itemHover");
}); 

Simplified HTML: (again, the divs in my markup aren't siblings or parents/children)

<div class="big1"></div>
<div class="big2"></div>
<div class="big3"></div>
<div class="small1"></div>
<div class="small1"></div>
<div class="small2"></div>
<div class="small2"></div>
<div class="small3"></div>
<div class="small3"></div>

UPDATE/SOLUTION: This is just to clarify a couple of lines in @Sergio's solution. There were a couple of missing brackets that were breaking the code in FF. The solution below was successfully tested in IE 10, Chrome and FF:

$('div[class^="small"]').hover(function () {
    var this_class = $(this).prop('class');
    var filter = $.grep(this_class, function (a) {
        return a ^= 'small';
    });
    $('div.big' + filter).addClass("itemHover");
}, function () {
    $('div[class^="big"]').removeClass("itemHover");
});
Sergio

Not sure how your html looks like but this can be useful:

Option 1: (this works with different classes)

$('div[class^="small"').hover(function () {
      var filter = $.grep(this.className.split(' '), function(a) {
           return a.indexOf('small')===0;
      });
    filter = filter[0].split('small')[1];
    $('div.big' + filter).addClass("itemHover");
}, function () {
    $('div[class^="big"').removeClass("itemHover");
});

Demo here (hover with mouse over S1)


Option 2:

var all_BIG = $('div[class^="big"');
var all_small = $('div[class^="small"');
all_small.hover(function () {
    var this_ind = $(this).index();
    all_BIG.eq(this_ind).addClass("itemHover");
}, function () {
    all_BIG.removeClass("itemHover");
});

Demo here (hover with mouse over S1)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

SCSS variable class name

分類Dev

SCSS variable class name

分類Dev

SCSS variable class name

分類Dev

Jquery variable.addClass()

分類Dev

Applying CSS on class with variable name

分類Dev

Python class variable name vs __name__

分類Dev

Can class name and variable name be same in java?

分類Dev

How would I create a class with a variable name in the class name?

分類Dev

Why javascript static variable not able to access ( using class name ) without creating minimum one instance?

分類Dev

Copy and rename files recursively using part of folder name for new name

分類Dev

C++ - Using a variable as the variable name in getline

分類Dev

Get The Class Name From an Object Variable

分類Dev

Using $(this).addClass is not working in AJAX success

分類Dev

Shell Script: Using files based on part of file name

分類Dev

Incrementing integer using variable name string

分類Dev

Python: creating a variable using retrieved string name

分類Dev

Using "?" in variable name for Objective-C development

分類Dev

Using a name of the system variable in the referenced DLL of CSPROJ

分類Dev

Using variable as table name in SQL pdo query

分類Dev

Using purrr::map2 when one variable is not part of the function

分類Dev

assign part of a scalar to another variable using regular expressions

分類Dev

Changing a class name in HTML using Tampermonkey

分類Dev

Retrieving an object's class name using reflection

分類Dev

Using a for loop to return variable name - variable mean in R

分類Dev

'dependent name is not a type' when using nested nested class in templated class

分類Dev

Passing a variable value, rather than its name, as a class

分類Dev

how to search for a variable using a string? / can you search a class for a variable?

分類Dev

Using a class variable as a default argument to the class member function

分類Dev

Undefined reference to class_name<template_argument>function_name(variable)?

Related 関連記事

  1. 1

    SCSS variable class name

  2. 2

    SCSS variable class name

  3. 3

    SCSS variable class name

  4. 4

    Jquery variable.addClass()

  5. 5

    Applying CSS on class with variable name

  6. 6

    Python class variable name vs __name__

  7. 7

    Can class name and variable name be same in java?

  8. 8

    How would I create a class with a variable name in the class name?

  9. 9

    Why javascript static variable not able to access ( using class name ) without creating minimum one instance?

  10. 10

    Copy and rename files recursively using part of folder name for new name

  11. 11

    C++ - Using a variable as the variable name in getline

  12. 12

    Get The Class Name From an Object Variable

  13. 13

    Using $(this).addClass is not working in AJAX success

  14. 14

    Shell Script: Using files based on part of file name

  15. 15

    Incrementing integer using variable name string

  16. 16

    Python: creating a variable using retrieved string name

  17. 17

    Using "?" in variable name for Objective-C development

  18. 18

    Using a name of the system variable in the referenced DLL of CSPROJ

  19. 19

    Using variable as table name in SQL pdo query

  20. 20

    Using purrr::map2 when one variable is not part of the function

  21. 21

    assign part of a scalar to another variable using regular expressions

  22. 22

    Changing a class name in HTML using Tampermonkey

  23. 23

    Retrieving an object's class name using reflection

  24. 24

    Using a for loop to return variable name - variable mean in R

  25. 25

    'dependent name is not a type' when using nested nested class in templated class

  26. 26

    Passing a variable value, rather than its name, as a class

  27. 27

    how to search for a variable using a string? / can you search a class for a variable?

  28. 28

    Using a class variable as a default argument to the class member function

  29. 29

    Undefined reference to class_name<template_argument>function_name(variable)?

ホットタグ

アーカイブ