从'this'对象中选择多个类名?

时间:2022-09-15 01:32:36

I'm trying to find a way to get all the class names from an object when I hover on it. Then, after getting the class names, append new css to all elements that contain those class names.

当我将鼠标悬停在对象上时,我正试图找到一种从对象中获取所有类名的方法。然后,在获取类名后,将新css附加到包含这些类名的所有元素。

http://jsfiddle.net/fr5q8c4v/

$( '.cell' ).hover(
function() {
   $(this).classNames.css( //something like this?
       "border", "solid 1px black"  
   );

}, function() {
    $(this).classNames.css( 
         "border", "solid 1px transparent"   
    )
});

In this example, when I hover over a cell with a class name of "one" I want to apply css to all cells with a class name of "one".

在这个例子中,当我将鼠标悬停在类名为“one”的单元格上时,我想将css应用于类名为“one”的所有单元格。

1 个解决方案

#1


2  

Here's one approach. You get the class name list, split them and take the last one. Then add/remove a class as you hover

这是一种方法。你得到了班级名单,拆分它们并取最后一个。然后在悬停时添加/删除一个类

var item;
$('.cell').hover(function () {
    item = '.'+$(this).attr('class').split(' ').pop();
    $(item).addClass("bordered");
}, function () {
    $(item).removeClass("bordered")
});

jsFiddle example

#1


2  

Here's one approach. You get the class name list, split them and take the last one. Then add/remove a class as you hover

这是一种方法。你得到了班级名单,拆分它们并取最后一个。然后在悬停时添加/删除一个类

var item;
$('.cell').hover(function () {
    item = '.'+$(this).attr('class').split(' ').pop();
    $(item).addClass("bordered");
}, function () {
    $(item).removeClass("bordered")
});

jsFiddle example