【问题标题】:jquery- how to define specific variable class for element have multiple classesjquery-如何为元素定义特定的变量类有多个类
【发布时间】:2014-07-30 18:33:17
【问题描述】:

我有元素,每个元素都有多个类,我想定义这些类中的每一个以在 jquery 中使用..

解释一下..这是代码:http://jsfiddle.net/wUWYs/2/

HTML:

<div class="red row4 col1"></div>
<div class="red row3 col2"></div>
<div class="red row2 col3"></div>
<div class="red row1 col4"></div>

<div class="blue row1 col1"></div>
<div class="blue row2 col2"></div>
<div class="blue row3 col3"></div>
<div class="blue row4 col4"></div>

这就是我尝试用 jquery 做的事情:

jQuery:

$(".red").each(function(){
    var colNumber = $(this).attr("class");
    $(this).hover(
        function(){$(".blue."+colNumber).show();},
        function(){$(".blue").hide();}
    );
});

当悬停red elementcol1row4 类时我想要的主要问题,然后将显示两个blue elements,谁有col1 和谁有@ 987654329@..

这里有问题:

var colNumber = $(this).attr("class");

如何定义具体的类,如何使数字可变?

【问题讨论】:

    标签: javascript jquery html css variables


    【解决方案1】:

    不使用jQuery,你看这个例子

     .red.col1:hover ~ .col1{
        display:block !important;
       }
     .red.col2:hover ~ .col2{
          display:block !important;
      }
    .red.col3:hover ~ .col3{
       display:block !important;
    }
    .red.col4:hover ~ .col4{
       display:block !important;
     }
     ![enter image description here][1]
    

    http://jsfiddle.net/kisspa/cfeSy/

    【讨论】:

    • 不错的解决方案,但我的原始代码比仅使用悬停动作更复杂,除了主要问题仍然存在:)
    • 你认为我的回答是假的
    • 对不起,你的回答是正确的,但我想要的是“如何为元素定义特定的变量类有多个类”..我的页面中的原始代码不是那么简单,它使用了几个任务具体取决于这个部分,而不仅仅是使用 CSS。不过,再次感谢您的回答。
    【解决方案2】:

    这是我的尝试:

    $(".blue").hide();
    var getClass = function(target){
      return $.map(target.className
                         .split(' '), function(e){                           
                           return e == 'red' ? null : '.blue.' + e;
                        }).join(', ');
    };
    $(".red").hover(function(e){       
        $(getClass(e.target)).show();
     }, function(e){
        $(getClass(e.target)).hide();
    });
    

    Demo.

    思路很简单,你只需要得到悬停的红色div的className,去掉"red",修剪它,用空格分割得到其他类的数组,比如["col1", "row4"],然后你就加入它可以制作".blue.col1, .blue.row4"这样的字符串,并使用这个字符串作为选择器来选择蓝色的div。

    【讨论】:

    • 太棒了!你能解释一下你做了什么吗?
    • @Ahmad 看到我的更新,很简单。还有其他一些解决方案,但我认为这是最简单的解决方案。您可能想了解更多关于 jQuery 方法 $.map().
    • @Ahmad 看我更新的demo,之前的demo有个bug,只有当你指定red为第一类时才有效,比如"red col1 row1",如果是"col1 red row1",演示将无法正常工作。更新后的演示完美运行。
    • 谢谢@King King,我会读到$.map(),但我有一个相对的小问题:如果我有div.row1div.row2div.row3...并且我想全部选择,是否有引用变量编号的快捷方式?像("row[*]") 这样的东西?!
    • @Ahmad 我们没有任何纯 CSS 选择器支持该要求,只有一个类似的选择器称为 attribute selector,但我认为在这种情况下它不适合(您可以执行div[class^=row] 之类的操作,但并不精确,例如它可以将divs 与class='rowL' 匹配。您应该使用filter 方法来过滤匹配的元素。查看this demo 了解更多。注意我这里使用了 RegExp,它是 Javascript 的重要组成部分,你应该阅读它,它对搜索、过滤很有帮助,替换,...​​
    【解决方案3】:

    改用索引属性:

    $(this).hover(
        function(){$(".blue:eq("+$(this).index()+")").show();},
        function(){$(".blue").hide();}
    );
    

    Working Demo

    【讨论】:

    • thanx,但我的意思是,当您将鼠标悬停在红色 col1 row4 上时,会出现两个蓝色元素:谁拥有“.col1”和谁拥有“.row4”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    • 2018-04-20
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    • 2012-05-15
    相关资源
    最近更新 更多