简体   繁体   English

jQuery更改列内文本的颜色

[英]JQuery changing colour of text within columns

I have three columns within my webpage. 我的网页中有三列。 On each column there are a group of three squares, the square represent a unique colour. 每列上都有一组三个正方形,正方形代表一种独特的颜色。 So when the user clicks on red in column 1 the text within column 1 goes red, if blue it will go blue. 因此,当用户单击第1列中的红色时,第1列中的文本变为红色,如果为蓝色,则它将变为蓝色。 If the user clicks on green within column 2 the text within column 2 will go green. 如果用户单击第2列中的绿色,则第2列中的文本将变为绿色。

I am new to jQuery so I am not sure if I have done this right and would like to know if this is the best way of doing it. 我是jQuery的新手,所以我不确定自己是否做对了,并想知道这是否是最好的方法。

What I want to know is there anyway of changing this so there is only one style called picker for all in each column. 我想知道的是,无论如何都需要进行更改,因此每一列中只有一种样式称为“选择器”。 Also can I change the jQuery so it's not 3 seperate functions, is there a more cleaner way of doing this? 我还可以更改jQuery,使其不是3个单独的函数,是否有更简洁的方法?

Thanks. 谢谢。

Yes! 是! CSS selectors are all reusable! CSS选择器都是可重用的! you shouldn't created multiple class' with the exact same attributes and values! 您不应该创建具有完全相同的属性和值的多个类!

all the css classes you need .col, .wrapper, .picker 您需要的所有CSS类.col, .wrapper, .picker

and then working with jQuery instead of using a div id when you want to use the code in mulitple places, work out where the element is relative to the element that fired the event or $(this) 然后在要在多个地方使用代码时使用jQuery而不是使用div id,请计算元素相对于触发事件的元素或$(this)

check out the fiddle http://jsfiddle.net/WJ5DZ/1/ 查看小提琴http://jsfiddle.net/WJ5DZ/1/

You may try this way: 您可以这样尝试:

$(function () {
    $('.picker').css('background-color', function () { //Setup the background color for each picker square.
        return $(this).data('color'); //get its color from data attribute
    }).click(function () { //Chain it through for the click event
        $(this).closest('.content').css({ //get the parent content element where color needs to be applied
            color: $(this).data('color') //set the color as that of the clicked picker.
        });
    });
});

Demo 演示

In the markup provide a class called say content to identify its own parent content. 在标记中,提供一个称为“说content的类以标识其自己的父内容。

<div class='col2 content' id="test1"> <!-- You don't need id if you are using this for selcting. 

Remove all the picker1, 2 css rules and have just the picker . 删除所有picker1, 2 CSS规则和刚才的picker

Using closest will ensure that even if you plan to add a wrapper to your picker still your actuall content div will be selected. 使用closest将确保即使您计划将包装器添加到选择器中,仍然会选择您的实际内容div。

yes there is.. change all you class(picker1,picker2) to picker and remove the id (test,test1..) 是的,有..将您所有的课程(picker1,picker2)更改为picker并删除ID(test,test1 ..)

try this 尝试这个

   $('.picker').each(function(){
      var myColor = $(this).data('color');
      $(this).css({background: myColor })
   });
  $('.picker').click(function(){
     var myColor = $(this).data('color');
     $(this).parent().css({color: myColor});

  });

NOTE: you don't need click event inside each loop 注意:您不需要each循环内的click事件

fiddle here 在这里摆弄

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM