js设置随机颜色

  var chars = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'];
  function generateMixed(n) {
    var res = "";
    for(var i = 0; i < n ; i ++) {
      var id = Math.ceil(Math.random()*16);
      res += chars[id];
    }
    return res;
  }
  $(document).ready(function () {
    var p_ = document.getElementsByClassName('sel')
    for (let i = 0; i < p_.length; i++) {
      var num1 = generateMixed(6);
      var num2 = generateMixed(6);
      p_[i].style.color = "#"+num1;
      p_[i].style.backgroundColor = "#"+num2;
    }
  })

posted @ 2023-08-04 15:25  fly_bk  阅读(55)  评论(0编辑  收藏  举报