【问题标题】:CSS for selected / active elements选定/活动元素的 CSS
【发布时间】:2014-05-14 18:48:27
【问题描述】:

我对 javascript 和 jquery 还是很陌生。我正在尝试创建一个板,用户在其中选择一个单元格(添加一个活动类),然后单击一个填充有某种颜色的不同单元格。然后原始单元格填充相同的颜色。这是我的jsfiddle:

http://jsfiddle.net/2J8yq/3/

var fillIn = $('.active_color_select')
$(red).click(function(){
 $(fillIn).css('background-color', 'red');
 });

** 我相信问题出在这里

如您所见,当您单击空单元格然后单击颜色时,单元格会填充,但随后选择的下一种颜色会将所有单元格更改为相同的颜色。我不确定如何在不切换下一个单元格时让选定的单元格具有所需的颜色。

任何意见都将不胜感激!

谢谢

【问题讨论】:

标签: jquery css


【解决方案1】:

我会这样写:

$('.color').click(function(){
    var bgColor = $(this).attr('id');
    $('.active_color_select').css('background-color',bgColor);
});

编辑:实际上,Rick 提出了一个很好的观点。也许最好从当前点击目标中获取 bg-color 属性,如下所示:

$('.color').click(function(){
    var bgColor = $(this).css('background-color');
    $('.active_color_select').css('background-color',bgColor);
});

【讨论】:

  • 请让它有小提琴它对所有者有用
  • 我不认为将颜色存储在 ID 中是一件好事,它并不是为此而生的。
【解决方案2】:

您已经使用选择器查询了该元素。

fillIn.css('background-color', 'red');

fillIn 是一个变量,表示从选择器查询返回的 jQuery 对象 ($('.active_color_select'))

所以整个事情看起来像:

var fillIn = $('.active_color_select')
$(red).click(function() {
    fillIn.css('background-color', 'red');
});

如果您想将其应用于一组元素,则执行以下操作可能会更容易:

<div class="fill-me-in" data-color="red"></div>
<div class="fill-me-in" data-color="green"></div>

然后你可以让它更通用,比如:

$('.fill-me-in').click(function(e) {
    var me    = $(this),
        color = me.data('color');
    me.css('background-color', color);
});

【讨论】:

    【解决方案3】:

    您需要使用$('.active_color_select') 而不是$(fillIn) 仅更改具有active_color_select 类的td 的颜色:

    $(red).click(function () {
        $('.active_color_select').css('background-color', 'red');
    });
    
    $(blue).click(function () {
        $('.active_color_select').css('background-color', 'blue');
    });
    
    $(green).click(function () {
        $('.active_color_select').css('background-color', 'green');
    });
    
    $(mint).click(function () {
        $('.active_color_select').css('background-color', '#9CBA7F');
    });
    
    $(white).click(function () {
        $('.active_color_select').css('background-color', 'white');
    });
    
    $(lightPurple).click(function () {
        $('.active_color_select').css('background-color', '#BF5FFF');
    });
    

    还请注意,您的所有变量都已经是一个 jQuery 对象。您无需再次将其包裹在 $() 中。

    例如,您可以使用:

    inactive.removeClass('active_color_select'); 
    

    代替:

    $(inactive).removeClass('active_color_select');
    

    其他变量也应采用同样的方法。

    Updated Fiddle

    【讨论】:

      【解决方案4】:

      在所有点击处理程序上,您需要删除所有活动类并再次使其处于非活动状态:

       $(red).click(function(){
          $(fillIn).css('background-color', 'red');
          $('.active_color_select').removeClass('active_color_select');
        });
      

      你也不需要所有这些变量就行了:

       $('#red').click(function(){
          $('.active_color_select').css('background-color', 'red');
          $('.active_color_select').removeClass('active_color_select');
        });
      

      您还可以链接调用:

       $('#red').click(function(){
          $('.active_color_select').css('background-color', 'red')
                                   .removeClass('active_color_select');
        });
      

      编辑,您的初始点击处理程序的范围也太大:

      $('.inactive').click(function(){
            $('.active_color_select').removeClass('active_color_select');
            //optionally you could add inactive back to the above
            $(this).addClass('active_color_select');
            //optionally you can remove inactive from this
      }) //close click handler here 
      

      【讨论】:

        【解决方案5】:

        这里有一些工作代码,可以满足您的需求,但效率更高 :) http://jsfiddle.net/2J8yq/9/

        // Get the fillable elements. $(selector).find() is faster than $(selector > selector)
        // http://stackoverflow.com/questions/8929951/jquery-selector-why-is-id-findp-faster-than-id-p
        var fillableElements = $('#master').find('td');
        var colorElements = $('#selection').find('.color');
        
        // A var for storing the currently selected elements
        var selected = null;
        
        // We use .on() if you are adding dynamic elements in the future, else click events won't fire.
        // https://api.jquery.com/on/
        fillableElements.on('click', function() {
            var el = $(this);
            // Remove from all other elements the selected class
            fillableElements.removeClass('active_color_select');
            // Add the selected class to the element
            el.addClass('active_color_select');    
            // Assign the selected value
            selected = el;
        
        });
        
        colorElements.on('click', function() {
            var el = $(this);
            // Get the bg color from the chosen color
            var color = el.css('background-color');
            // Add the color
            selected.css('background-color', color);
        });
        

        【讨论】:

          猜你喜欢
          • 2013-09-14
          • 2020-01-15
          • 1970-01-01
          • 1970-01-01
          • 2014-12-04
          • 1970-01-01
          • 1970-01-01
          • 2016-01-14
          • 2017-05-23
          相关资源
          最近更新 更多