Css Wordpress-更改多个div的背景色

Css Wordpress-更改多个div的背景色,css,wordpress,background-color,jsfiddle,Css,Wordpress,Background Color,Jsfiddle,我的页面上有多个DIV元素,类为“grid item container” 我想让每一个的背景颜色不同。我将设置一个5种不同颜色的阵列,可以设置 这里有一个脚本似乎可以做到这一点: 然而,我无法让它在我的页面上运行。这个脚本中有什么东西需要修改以使其对Wordpress友好吗 问候 Dave你可能不想用这样的东西来包装它: jQuery(document).ready(function($) { // Inside of this function, $() will work as an

我的页面上有多个DIV元素,类为“grid item container”

我想让每一个的背景颜色不同。我将设置一个5种不同颜色的阵列,可以设置

这里有一个脚本似乎可以做到这一点:

然而,我无法让它在我的页面上运行。这个脚本中有什么东西需要修改以使其对Wordpress友好吗

问候
Dave

你可能不想用这样的东西来包装它:

jQuery(document).ready(function($) {
   // Inside of this function, $() will work as an alias for jQuery()
   // and other libraries also using $ will not be accessible under this shortcut
});
WordPress附带的jQuery库被设置为noConflict()模式(请参阅wp includes/js/jQuery/jQuery.js)。这是为了防止与WordPress可以链接的其他JavaScript库的兼容性问题。阅读更多关于它的法典


另外,将$(.random)更改为$(.grid item container),这是针对您的div类的。

因此我将其添加到我的头文件中:jQuery(document).ready(函数($){var randomColor=[“绿色”、“黄色”、“红色”、“蓝色”、“橙色”、“粉色”、“青色”];$(.random”)。每个(函数(索引){var len=randomColors.length;var randomNum=Math.floor(Math.random()*len);$(this.css(“backgroundColor”,randomColors[randomNum]);//从数组中删除颜色,这样就不能再使用randomColors.splice(randomNum,1);});谢谢Tim。太好了。
jQuery(document).ready(function($) {
   // Inside of this function, $() will work as an alias for jQuery()
   // and other libraries also using $ will not be accessible under this shortcut
});