最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Get Bootstrap's current theme colors in Javascript - Stack Overflow

programmeradmin0浏览0评论

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)

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:

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.

Is there a better way to get the current theme colors from the CSS using Javascript? Or is there no way without creating a DOM element and testing it's background color?

If there is an easy way to get CSS rules using Javascript without dealing with a DOM element, that'd be great.

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)

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:

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.

Is there a better way to get the current theme colors from the CSS using Javascript? Or is there no way without creating a DOM element and testing it's background color?

If there is an easy way to get CSS rules using Javascript without dealing with a DOM element, that'd be great.

Share Improve this question asked Oct 2, 2018 at 7:27 SkeetsSkeets 4,8283 gold badges47 silver badges73 bronze badges 3
  • using only js or also jquery? – לבני מלכה Commented Oct 2, 2018 at 7:31
  • Bootstrap requires jquery, so jquery is included. – Skeets Commented Oct 2, 2018 at 7:42
  • Similar question: stackoverflow.com/questions/52024981/… – Carol Skelly Commented Oct 2, 2018 at 9:14
Add a comment  | 

2 Answers 2

Reset to default 20

Bootstrap stores its themed colors also inside CSS variables on the :root element.

A more appropriate way would be to extract these colors using getComputedStyle() and getPropertyValue('--variable')

const style = getComputedStyle(document.body);
const theme = {
  primary: style.getPropertyValue('--primary'),
  secondary: style.getPropertyValue('--secondary'),
  success: style.getPropertyValue('--success'),
  info: style.getPropertyValue('--info'),
  warning: style.getPropertyValue('--warning'),
  danger: style.getPropertyValue('--danger'),
  light: style.getPropertyValue('--light'),
  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

element.style.backgroundColor = theme.primary; // Javascript
$(element).css('background-color', theme.primary); // jQuery

For bootstrap 5

Bootstrap 5 renamed it properties, to use above code with Bootstrap 5 see example below

const style = getComputedStyle(document.body);
const theme = {
  primary: style.getPropertyValue('--bs-primary'),
  secondary: style.getPropertyValue('--bs-secondary'),
  success: style.getPropertyValue('--bs-success'),
  info: style.getPropertyValue('--bs-info'),
  warning: style.getPropertyValue('--bs-warning'),
  danger: style.getPropertyValue('--bs-danger'),
  light: style.getPropertyValue('--bs-light'),
  dark: style.getPropertyValue('--bs-dark'),
};

console.log(theme);
<!-- JavaScript Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-A3rJD856KowSb7dwlZdYEkO39Gagi7vIsF0jrRAoQmDKKtQBHUuLZ9AsSv4jD4Xa" crossorigin="anonymous"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">

<div class="container"></div>

Use document.body.getElementsByTagName("*") to get all DOM in body and loop it to get what you want

    $(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 >

发布评论

评论列表(0)

  1. 暂无评论