JavaScript將讓你訪問應用到文檔的所有樣式。檢查每一個,看它是否包含顏色。
CSS
<style>
.first-selector div
{
color: #3C3C3C;
}
.second-selector span
{
background-color: #3C3C3C;
}
.third-selector
{
border: 1px solid #3C3C3C;
}
.fourth-selector .nothing
{
color: red;
}
</style>
JS:
<script>
function getColorRules(color)
{
//convert hex to rgb, since hex color styles are internally stored as rgb
if (/[#]{0,1}[0-9A-F]{6}/.test(color))
{
color = color.replace('#', '');
var r = parseInt(color.substr(0, 2), 16);
var g = parseInt(color.substr(2, 2), 16);
var b = parseInt(color.substr(4, 2), 16);
color = "rgb(" + r + ", " + g + ", " + b + ")";
}
var returnArray = [];
//grab all stylesheets
var sheets = document.styleSheets;
for (var i in sheets) {
//to work in FF or chrome
var rules = sheets[i].rules || sheets[i].cssRules;
for (var r in rules)
{
//console.log(rules[r]);
if (rules[r].cssText !== undefined) { //ignore empty or non css properties
if (rules[r].cssText.indexOf(color) > -1) //if the color is found in the style rule
{ //add it to an array
var style = {
selector: rules[r].selectorText,
style: rules[r].style.cssText.split(":")[0]
};
returnArray.push(style);
}
}
}
}
return returnArray;
}
console.log(getColorRules("#3C3C3C"));
//[{ selector=".first-selector div", style="color"}, { selector=".second-selector span", style="background-color"}, { selector=".third-selector", style="border"}]
console.log(getColorRules("red"));
// [{ selector=".fourth-selector .nothing", style="color"}]
</script>
如果你說 「搞定」
,沒有PHP或Java參與。 – 2014-11-04 11:42:57
以及「java」如何與網頁建設相關? – 2014-11-04 11:43:31
當你說Java時,你碰巧指的是jQuery? – user2209644 2014-11-04 11:44:00