Using Bootstrap Colors in JS

跟風遠走 提交于 2020-01-04 21:31:03

问题


I am using a Google-Chart and want to set the slice colors to the color used by bootstrap, e.g., for badge-success or badge-danger. Is there any way to access these color-codes from within JavaScript?


回答1:


This works if you're using Bootstrap 4:

const root = document.querySelector('html');
getComputedStyle(root).getPropertyValue('--danger');

You can access the CSS variables present in the :root pseudoelement




回答2:


Another approach is...

document.body.innerHTML += '<div id="myBadge" class="badge-danger"></div>';
var elem = document.getElementById("myBadge");
var dangerColor = window.getComputedStyle(elem).getPropertyValue("background-color");
elem.parentNode.removeChild(elem);

Demo: https://www.codeply.com/go/GC96hjbf5v



来源:https://stackoverflow.com/questions/52024981/using-bootstrap-colors-in-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!