[英]Get Bootstrap's current theme colors in Javascript
My objective is to create charts using chart.js
using colors that match the current chosen Bootstrap 4 theme (there are multiple available for the site)我的目标是使用与当前选择的 Bootstrap 4 主题匹配的颜色使用chart.js
创建图表(该站点有多个可用)
My current strategy is to have a badge of every color on the page:我目前的策略是在页面上使用每种颜色的徽章:
<div id="color-example-badge-primary" class="badge badge-primary" ></div >
<div id="color-example-badge-warning" class="badge badge-warning" ></div >
<div id="color-example-badge-danger" class="badge badge-danger" ></div >
<div id="color-example-badge-secondary" class="badge badge-secondary" ></div >
<div id="color-example-badge-light" class="badge badge-light" ></div >
<div id="color-example-badge-success" class="badge badge-success" ></div >
<div id="color-example-badge-info" class="badge badge-info" ></div >
<div id="color-example-badge-dark" class="badge badge-dark" ></div >
and then use jQuery to pull the background colors into an array:然后使用 jQuery 将背景颜色拉入数组:
var themeColors = [$("#color-example-badge-primary").css('backgroundColor'),
$("#color-example-badge-warning").css('backgroundColor'),
$("#color-example-badge-danger").css('backgroundColor'),
$("#color-example-badge-secondary").css('backgroundColor'),
$("#color-example-badge-light").css('backgroundColor'),
$("#color-example-badge-success").css('backgroundColor'),
$("#color-example-badge-info").css('backgroundColor'),
$("#color-example-badge-dark").css('backgroundColor')
];
I can then give this array to chart.js
to create a chart with the theme colors.然后我可以将这个数组提供给chart.js
来创建一个带有主题颜色的图表。
Is there a better way to get the current theme colors from the CSS using Javascript?有没有更好的方法可以使用 Javascript 从 CSS 中获取当前主题颜色? Or is there no way without creating a DOM element and testing it's background color?或者没有创建 DOM 元素并测试它的背景颜色就没有办法?
If there is an easy way to get CSS rules using Javascript without dealing with a DOM element, that'd be great.如果有一种简单的方法可以使用 Javascript 获取 CSS 规则,而无需处理 DOM 元素,那就太好了。
Bootstrap stores its themed colors also inside CSS variables on the :root
element. Bootstrap 也将其主题颜色存储在:root
元素的 CSS 变量中。
A more appropriate way would be to extract these colors using getComputedStyle()
and getPropertyValue('--variable')
更合适的方法是使用getComputedStyle()
和getPropertyValue('--variable')
提取这些颜色
var style = getComputedStyle(document.body); var theme = {}; theme.primary = style.getPropertyValue('--primary'); theme.secondary = style.getPropertyValue('--secondary'); theme.success = style.getPropertyValue('--success'); theme.info = style.getPropertyValue('--info'); theme.warning = style.getPropertyValue('--warning'); theme.danger = style.getPropertyValue('--danger'); theme.light = style.getPropertyValue('--light'); theme.dark = style.getPropertyValue('--dark'); console.log(theme);
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"> <div class="container"></div>
Now you can get these use these colors with JavaScript
or jQuery
as follow现在您可以使用JavaScript
或jQuery
获取这些颜色,如下所示
element.style.backgroundColor = theme.primary; // Javascript
$(element).css('background-color', theme.primary); // jQuery
Use document.body.getElementsByTagName("*")
to get all DOM in body
and loop it to get what you want使用document.body.getElementsByTagName("*")
获取body
所有 DOM 并循环它以获得你想要的
$(document).ready(function(){ var items = document.body.getElementsByTagName("*"); var arr=[]; for (var i = 4, len = items.length; i < len; i++) { arr.push(document.getElementById(items[i].id)!=null?$('#'+items[i].id).css('backgroundColor'):""); } console.log(arr) });
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script> <div id="color-example-badge-primary" class="badge badge-primary" >a</div > <div id="color-example-badge-warning" class="badge badge-warning" >a</div > <div id="color-example-badge-danger" class="badge badge-danger" >a</div > <div id="color-example-badge-secondary" class="badge badge-secondary" >a</div > <div id="color-example-badge-light" class="badge badge-light" >a</div > <div id="color-example-badge-success" class="badge badge-success" >a</div > <div id="color-example-badge-info" class="badge badge-info" >a</div > <div id="color-example-badge-dark" class="badge badge-dark" >a</div >
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.