Using Bootstrap Colors in JS

2019-07-08 10:17发布

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?

2条回答
等我变得足够好
2楼-- · 2019-07-08 10:51

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

查看更多
爷的心禁止访问
3楼-- · 2019-07-08 10:57

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

查看更多
登录 后发表回答