简体   繁体   English

解决divs JavaScript的背景色

[英]Addressing background color of divs JavaScript

I am trying to target multiple div s and randomly change the background color, (or other attributes), all at once. 我正在尝试定位多个div并一次随机更改背景颜色(或其他属性)。 I've been able to target the body background with document.body.style.background but I'm not sure this would work with multiple elements. 我已经能够使用document.body.style.background定位身体背景,但是我不确定这是否可以与多个元素一起使用。 Any insight would be nice. 任何见解都会很好。

Here is a bit of code that I'm trying to accomplish that with: 这是我尝试使用的一些代码:

 function randBGcolor() {
    var letters = '0123456789ABCDEF';
    var color = '#';
        for (var i=0; i<6; i++ ) {
            color += letters[Math.round(Math.random() * 15)];
        }
   document.body.style.background = color;
}

I would like to still use the random color function, but have it apply it to many elements on one click. 我仍想使用随机颜色功能,但只需单击一下即可将其应用于许多元素。

using jQuery and your function: 使用jQuery和您的函数:

function randBGcolor() {
   var letters = '0123456789ABCDEF';
   var color = '#';
      for (var i=0; i<6; i++ ) {
         color += letters[Math.round(Math.random() * 15)];
      }
   return color;
}

$('#button').on('click', function() {
    $('div.classname').css('background-color', randBGcolor());
})

I think you can accomplish this with JQuery, you may be able to find someone who knows how in pure JS. 我认为您可以使用JQuery完成此工作,也许可以找到知道纯JS知识的人。

You can loop through multiple elements, say div tags by using each() 您可以使用each()遍历多个元素,例如div标签

http://jsfiddle.net/Kwb49/ - this example changes all divs when clicking the body http://jsfiddle.net/Kwb49/-此示例在单击主体时更改了所有divs

$(document).ready(function() {

    $('body').click(function() {
       $('div').each(function() {
            var bgcolor = randBGcolor();
            $(this).css({ "background" : bgcolor });
        }); 
    });

});

function randBGcolor() {
    var letters = '0123456789ABCDEF';
    var color = '#';
    for (var i=0; i<6; i++ ) {
        color += letters[Math.round(Math.random() * 15)];
    }
    return color;
}

Pure JS solution: 纯JS解决方案:

http://jsfiddle.net/Kwb49/3/ (updated fiddle, works on click, added one test button) http://jsfiddle.net/Kwb49/3/ (更新了小提琴,可以单击,添加了一个测试按钮)

function randBGcolor() {
    var letters = '0123456789ABCDEF';
    var color = '#';
        for (var i=0; i<6; i++ ) {
            color += letters[Math.round(Math.random() * 15)];
        }

    return color;

}



 divs=document.getElementsByTagName('div');

    for(i=0;i<divs.length;i++) {
    divs[i].style.backgroundColor=randBGcolor();
    }

The css method can be used with a callback function, so that you can select all the elements that you want to change, and get a separate background color for each one with a single call: css方法可与回调函数一起使用,以便您可以选择要更改的所有元素,并通过一次调用为每个元素获得单独的背景色:

function randBGcolor() {
  var letters = '0123456789ABCDEF';
  var color = '#';
  for (var i = 0; i < 6; i++) {
    color += letters[Math.floor(Math.random() * letters.length)];
  }
  return color;
}

$('.someelements').css('background-color', randBGcolor);

Demo: http://jsfiddle.net/Guffa/Tp856/ 演示: http//jsfiddle.net/Guffa/Tp856/

To use it in a click event, you just use the click method inside the ready event. 要在click事件中使用它,只需在ready事件中使用click方法。 Example: 例:

$(document).ready(function(){
  $('.someelement').click(function(){
    $('.someelements').css('background-color', randBGcolor);
  });
});

Note: You should not use Math.random when you are calculating the random number. 注意:计算随机数时, 请勿使用Math.random That will make the lowest and highest values be selected half as often as the others. 这将使最低和最高值的选择频率是其他值的一半。 Use Math.floor and mulitply with a number that is one higher. Math.floor和mulitply与更高的数字一起使用。

One approach I'd suggest, in compliant browsers, using plain JavaScript is: 我建议在兼容的浏览器中使用纯JavaScript的一种方法是:

function randColor(n) {
    var result = [];
    for (var i = 0; i < n; i++) {
        result.push(Math.floor(Math.random() * 255));
    }
    return result.join(',');
}

function randBGColor () {
    return 'rgb(' + randColor(3) + ')';
}

var els = document.querySelectorAll('div');

[].forEach.call(els, function(a) {
    a.style.backgroundColor = randBGColor();
});

JS Fiddle demo . JS小提琴演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM