随机边框颜色

发布于 2024-08-16 07:04:49 字数 164 浏览 9 评论 0原文

我正在为 tumblr 网站设置主题,发布的每个图像都包含在“.photobox”类中。是否有一段 javascript 或 jQuery 我可以用来将 CSS 中“.photobox”的“border-color”属性更改为随机颜色 - 最好从预定义颜色列表中选择?我在这里找到了一两个脚本,但它们似乎不起作用。

I'm in the middle of theming a tumblr site, and each image posted is contained within a ".photobox" class. Is there a piece of javascript or jQuery i can use to change the "border-color" attribute of ".photobox" in the CSS to a random color - preferably chosen from a list of pre-defined colours? I've found one or two scripts on here but they don't seem to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

天气好吗我好吗 2024-08-23 07:04:49

自动更改间隔

setInterval(function(){

  // define our colors
  var colors = ["#CCCCCC","#333333","#990099"];
  // get a reference to <div id="mybox"></div>
  var myBox  = document.getElementById("mybox");
  // get a random color from list
  var rand   = Math.floor(Math.random()*colors.length);
  // set random color as borderColor
  myBox.style.borderColor = colors[rand];

}, 500); // run twice a second

一次性随机颜色

  // define our colors
  var colors = ["#CCCCCC","#333333","#990099"];
  // get a reference to <div id="mybox"></div>
  var myBox  = document.getElementById("mybox");
  // get a random color from list
  var rand   = Math.floor(Math.random()*colors.length);
  // set random color as borderColor
  myBox.style.borderColor = colors[rand];

单个容器内的许多图像

  // define our colors
  var colors = ["#CCCCCC","#333333","#990099"];
  // get all of our images within a specified container element
  var images = document.getElementById("container").getElementsByTagName("img");
  // loop through each
  for (var i = 0; i < images.length; i++) {
    // get a random color from list
    var rand   = Math.floor(Math.random()*colors.length);
    // apply random color to current image
    images[i].style.borderColor = colors[rand];
  }

jQuery 解决方案

$(".photobox").each(function(){
  var colors = ["#CCCCCC","#333333","#990099"];
  var rand = Math.floor(Math.random()*colors.length);
  $(this).css("borderColor", colors[rand]);
});

Auto-changing Intervals

setInterval(function(){

  // define our colors
  var colors = ["#CCCCCC","#333333","#990099"];
  // get a reference to <div id="mybox"></div>
  var myBox  = document.getElementById("mybox");
  // get a random color from list
  var rand   = Math.floor(Math.random()*colors.length);
  // set random color as borderColor
  myBox.style.borderColor = colors[rand];

}, 500); // run twice a second

One-time Random Color

  // define our colors
  var colors = ["#CCCCCC","#333333","#990099"];
  // get a reference to <div id="mybox"></div>
  var myBox  = document.getElementById("mybox");
  // get a random color from list
  var rand   = Math.floor(Math.random()*colors.length);
  // set random color as borderColor
  myBox.style.borderColor = colors[rand];

Many Images within Single Container

  // define our colors
  var colors = ["#CCCCCC","#333333","#990099"];
  // get all of our images within a specified container element
  var images = document.getElementById("container").getElementsByTagName("img");
  // loop through each
  for (var i = 0; i < images.length; i++) {
    // get a random color from list
    var rand   = Math.floor(Math.random()*colors.length);
    // apply random color to current image
    images[i].style.borderColor = colors[rand];
  }

jQuery Solution

$(".photobox").each(function(){
  var colors = ["#CCCCCC","#333333","#990099"];
  var rand = Math.floor(Math.random()*colors.length);
  $(this).css("borderColor", colors[rand]);
});
百变从容 2024-08-23 07:04:49

JavaScript 无法更改 CSS 规则集。您必须声明一个新的 CSS 规则集并将其分配给 DIV 或每个图像。喜欢

<script type="text/javascript">
    function changeClass()
    {
        document.getElementById("MyElement").className += " MyClass";
    }
</script>
...
<button onclick="changeClass()">My Button</button>

Javascript can not change CSS ruleset. You will have to declare a new CSS ruleset and assign it to DIV or to each image. Like

<script type="text/javascript">
    function changeClass()
    {
        document.getElementById("MyElement").className += " MyClass";
    }
</script>
...
<button onclick="changeClass()">My Button</button>
楠木可依 2024-08-23 07:04:49

就这样:http://jsbin.com/afadu

$(function(){ 
  var colors = ['#ff6','#6ff','#f6f','#f66','#66f','#6f6']; 
  $('input').click(function(){ 
      var randomcolor=Math.floor(Math.random()*colors.length); 
      //alert(randomcolor); 
      $('body').css('color',colors[randomcolor]); 
  }); 
});

There you go: http://jsbin.com/afadu

$(function(){ 
  var colors = ['#ff6','#6ff','#f6f','#f66','#66f','#6f6']; 
  $('input').click(function(){ 
      var randomcolor=Math.floor(Math.random()*colors.length); 
      //alert(randomcolor); 
      $('body').css('color',colors[randomcolor]); 
  }); 
});
能否归途做我良人 2024-08-23 07:04:49

我不确定,这可能是可能的 http://plugins.jquery.com/node/ 11985 下载 zip 并查看文件的源代码。它用于表格,颜色是随机的,但我认为我们可以给出固定的颜色代码

I'm not sure, it may be possible by this http://plugins.jquery.com/node/11985 download zip and see source code of file. It;s for table and colors are random but i think we can give fixed color code

甜味超标? 2024-08-23 07:04:49

根据您的评论,您想要这样的内容:

function getElementsByClassName ( classname, node ) {
    if ( !node ) {
        node = document.getElementsByTagName ( "body" )[0];
    }

    var a = [];
    var re = new RegExp ( '\\b' + classname + '\\b' );
    var els = node.getElementsByTagName ( "*" );
    for ( var i = 0, j = els.length; i < j; i++ ) {
        if ( re.test ( els[i].className ) ) {
            a.push ( els[i] );
        }
    }

    return a;
}

var photoboxes = getElementsByClassName ( 'photobox' );
var colors = Array ( 'red', 'blue', 'green', 'pink' );
for ( var i = 0; i < photoboxes.length; i++ ) {
    var images = photoboxes[i].getElementsByTagName ( 'img' );

    for ( var j = 0; j < images.length; j++ ) {
        images[j].style.borderColor = colors[Math.floor ( Math.random () * colors.length )];
    }
}

getElementByClassName 是从 此处

Based on your comment you want something like this:

function getElementsByClassName ( classname, node ) {
    if ( !node ) {
        node = document.getElementsByTagName ( "body" )[0];
    }

    var a = [];
    var re = new RegExp ( '\\b' + classname + '\\b' );
    var els = node.getElementsByTagName ( "*" );
    for ( var i = 0, j = els.length; i < j; i++ ) {
        if ( re.test ( els[i].className ) ) {
            a.push ( els[i] );
        }
    }

    return a;
}

var photoboxes = getElementsByClassName ( 'photobox' );
var colors = Array ( 'red', 'blue', 'green', 'pink' );
for ( var i = 0; i < photoboxes.length; i++ ) {
    var images = photoboxes[i].getElementsByTagName ( 'img' );

    for ( var j = 0; j < images.length; j++ ) {
        images[j].style.borderColor = colors[Math.floor ( Math.random () * colors.length )];
    }
}

The getElementByClassName was copied from here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文