English 中文(简体)
如何增加随机类别,将清单联系起来
原标题:How to add random class to link list

我的结构如下。

<ul id="dyn_nav">
    <li><a href="#">Home</a></li>
    <li><a href="#">Services</a></li>
    <li><a href="#">Contact</a>
        <ul class="submenu">
            <li><a href="#">Home</a></li>
            <li><a href="#">Services</a></li>
            <li><a href="#">Contact</a></li>
        </ul>
    </li>
</ul>

第一级(ul#dyn_nav>li)应从特定名单中随机分类。

班级为蓝色、绿色、大米、yan、红等......

我想使这种菜单不会固定。

就像一个人能够有头脑色绿色一样,另一个则有蓝色。 这只是我的想法。

页: 1

最佳回答
$(document).ready(function() {
    var classes = [ blue ,  green ,  magenta ,  cyan ,  red ];
    $( ul#dyn_nav>li ).each(function(i) {
        $(this).addClass(
            classes[Math.floor(Math.random()*classes.length)]);
    });
});

我还在“彩色:继承”、“<>>>>条码”上添加“彩色”一词,使彩色能够影响<条码>上的联系,而不仅仅是子弹。

问题回答

http://javascript.about.com/library/blsort2.htm

Dutow是正确的。 @Waz Design: 你们还可以尝试这一幻灯:

function rand(){ return (Math.round(Math.random())-0.5); }
    var colors = new Array( blue , yellow , red , green );//you can add more class here
    var init = colors.sort( rand );
    $( ul li ).each(function(i,el){ 
     $(el).addClass(init[i]); 
   });




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签