简体   繁体   English

使用纯Js或jquery获得64个类的相同CSS属性值

[英]Get same CSS property value of 64 classes with pure Js or jquery

I have created 64 color buttons in dom by js. 我已经通过js在dom中创建了64个颜色按钮。 they have class name like this: 他们有这样的类名:

c1
c2
c3
c4

and so on.. 等等..

I have created this by this codes: 我是通过以下代码创建的:

var eg_color_ul_1 = $('.eg-ul-1');
var eg_color_ul_2 = $('.eg-ul-2');

for (var linum = 1; linum < 65; linum++) {
    var Cselector = ".c" + linum;
    var colorMenu = $(Cselector).css( "background-color" );

    eg_color_ul_1.append("<a class=\"c" + linum + "\" color-code=\""+ colorMenu +"\"></a>"),
    eg_color_ul_2.append("<a class=\"c" + linum + "\" color-code=\""+ colorMenu +"\"></a>");

}

I have already set all color in css style sheet. 我已经在CSS样式表中设置了所有颜色。 like this: 像这样:

.c1 {
    background-color: #F44336;
}
.c2 {
    background-color: #E91E63;
}
.c3 {
    background-color: #9C27B0;
}
.c4 {
    background-color: #673AB7;
}

Now i dont get the color in dom. 现在我不了解dom的颜色。 it show color-code="undefined" 它显示color-code="undefined"

How can I fix it? 我该如何解决?

I am sorry for the title. 头衔很抱歉。 I can't understand what should be the title. 我不明白标题应该是什么。 So I put this. 所以我把这个。

Edit: 编辑:
Due to a answer from @Abdul I have corrected my js. 由于@Abdul的回答,我已更正了我的js。 But still it shows color-code="undefined" . 但是它仍然显示color-code="undefined" When I console log this it shows 当我控制台日志时,它显示

rgb(244, 67, 54)                                              main.js:10 
63 undefined                                                  main.js:10

this line is wrong: 这行是错误的:

var colorMenu = $(Cselector).css( "background-color" );

should be: 应该:

var colorMenu = $('.' + Cselector).css( "background-color" );

OR keep your line but change this: 保持您的台词,但更改此:

var Cselector = ".c" + linum;
                 ^ notice the period

see this fiddle 看到这个小提琴

I would also refactor your code: 我还将重构您的代码:

https://jsfiddle.net/6551a0ku/2/ https://jsfiddle.net/6551a0ku/2/

var eg_color_ul_1 = $('.eg-ul-1');
var eg_color_ul_2 = $('.eg-ul-2');

for (var linum = 1; linum < 5; linum++) {
    var className = 'c' + linum;
    var Cselector = '.' + className;
    var colorMenu = $(Cselector).css( "background-color" );

    eg_color_ul_1.append(getATag(className, colorMenu)),
    eg_color_ul_2.append(getATag(className, colorMenu));

}

function getATag(className, colorMenu) {
  var aTag = "<a class='"
  + className
  + "' color-code='"
  + colorMenu 
  + "'>a</a>";
  return aTag;
}

I think there is a misunderstanding of where jQuery will pull the background-color property from. 我认为对jQuery从何处提取background-color属性存在误解。 The DOM/jQuery doesn't have a direct understanding of the CSS properties you set down until you associate them with a DOM element either in the actual DOM, or in a Document Fragment. 直到您将CSS /属性与实际DOM或文档片段中的DOM元素相关联,DOM / jQuery 才对您设置的CSS属性没有直接的了解。

var eg_color_ul_1 = $('.eg-ul-1');
var eg_color_ul_2 = $('.eg-ul-2');

for (var linum = 1; linum < 65; linum++) {
    var Cselector = ".c" + linum;
    // the problem is here, at this point, there are no dom elements that match .cN so there is no value to return
    var colorMenu = $(Cselector).css( "background-color" );

    eg_color_ul_1.append("<a class=\"c" + linum + "\" color-code=\""+ colorMenu +"\"></a>"),
    eg_color_ul_2.append("<a class=\"c" + linum + "\" color-code=\""+ colorMenu +"\"></a>");

}

You can fix this by appending the anchor tags to the DOM first and then assigning the color code attribute. 您可以通过以下方式解决此问题:首先将锚标记附加到DOM, 然后分配颜色代码属性。

var eg_color_ul_1 = $('.eg-ul-1');
var eg_color_ul_2 = $('.eg-ul-2');

for (var linum = 1; linum < 65; linum++) {
    eg_color_ul_1.append("<a class=\"c" + linum + "\"></a>"),
    eg_color_ul_2.append("<a class=\"c" + linum + "\"></a>");
}

for (var linum = 1; linum < 65; linum++) {
    var domElement = $(".c" + linum)
    var colorMenu = domElement.css("background-color")
    domElement.attr('color-code', colorMenu)
}

I am unfamiliar with JQuery but if element.css("property") is the equivalent of element.style.property then that's your problem, the style there is referring to the inline style attribute and, as you are using a stylesheet, this is returning nothing. 我不熟悉JQuery,但是如果element.css("property")element.style.property等效,那么这就是您的问题,那里的style引用了内联style属性,并且,当您使用样式表时,这就是一无所获。

The solution is to instead use getComputedStyle() , like so: 解决方案是改为使用getComputedStyle() ,如下所示:

var eg_color_ul_1=$(".eg-ul-1");
var eg_color_ul_2=$(".eg-ul-2");
for(var linum=1;linum<65;linum++){
  var colorMenu=window.getComputedStyle($(".c"+linum),null).getPropertyValue("background-color");
  eg_color_ul_1.append("<a class=\"c"+linum+"\" data-color-code=\""+colorMenu+"\"></a>");
  eg_color_ul_2.append("<a class=\"c"+linum+"\" data-color-code=\""+colorMenu+"\"></a>");
}

I'd also suggest using data attributes rather than custom attributes to avoid any potential issues. 我还建议使用数据属性而不是自定义属性,以避免任何潜在的问题。

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

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