2012-07-16 3 views
1

나는 여러 색상을 CSS로 정의합니다. 이제는 모든 색상을 가져오고 모든 색상을 가진 새로운 객체를 작성하기위한 몇 가지 검사를하고 싶습니다.이 경우에는 데이터가있는 객체가 필요합니다. 런타임에 처음 두 번째와 세 번째 색상의 색 가능합니까?자바 스크립트에서 CSS에서 데이터 찾기

<html> 
<head> 
    <style> 

    p.first { color: gray; } 
    p.second { color: red; } 
    p.third { 
    background: purple; 
    color: white; 
    } 

    </style> 
</head> 
<body> 

답변

1

jQuery 이렇게하는 것이 쉬운 일이 아닙니다.

var colors = {firstcolor:jQuery("p.first").css("color"), secondcolor:jQuery("p.second").css("color")}; 

참고 : 귀하의 질문에 상태로

var color = jQuery("p.first").css("color"); 

객체를 설정하려면 : 당신이 jQuery를로드 일단

, 당신은이 같은 간단한 (jQuery.css을 활용)을 할 수 : RGB 값을 반환합니다. This Stackoverflow Answer

1

jQuery를하지 않고 작업을 수행하려면 참조 진수로 변환하는 것은 또한 매우 간단합니다 :

var p = document.getElementsByTagName('p'); 
var first = p.getElementsByClassName('first'); 
console.log(first.item(0).style.color); // etc 
관련 문제