2010-08-04 4 views
1

임의의 목록 집합을 표시하려면 어떻게합니까 항목이 있다고 생각합니다. 감사합니다.랜덤 디스플레이의 NULL 표시

// 객체의 li 수를 계산하십시오. var listCount = $ ("li.contentBlock", obj) .length;

 //Generate a random number from the count of li's 
     var randomListNumber = Math.floor(Math.random() * listCount -1); 

     //Generate list of items based on the amount of li's that are present 
     var firstList = "<li class='contentBlock'>"+$("li:eq("+randomListNumber+")", obj).html()+"</li>"; 

     //Target element that contains that list 
     var place = $("ul", obj).html(); 

     //Combine ul container and list generate 
     var newPlace = firstList+place; 

     //Replace current ul and li with newly generated random list 
     $("ul", obj).html(newPlace); 
+0

명확히하십시오. – Zaz

+0

페이지로드시 임의로 ul 내의 목록 항목을 생성하려고합니다. obj는이 콘텐츠 갤러리에 대한 var입니다. – Chris

답변

0

이와 비슷한? 예를 들어 .html 파일로 저장하십시오.

<html> 
<head> 
    <script src="http://code.jquery.com/jquery-1.4.2.min.js"></script> 
    <script> 
     $(document).ready(function(){ 
      var len = $("ul.contentBlock li").length;  

      $("ul.contentBlock li").each(function(){ 
       $(this).html(Math.floor(Math.random() * len)); 
      }); 

     }); 
    </script> 
</head> 
<body> 
    <ul class="contentBlock"> 
     <li></li> 
     <li></li> 
     <li></li> 
     <li></li> 
    </ul> 
</body> 
</html>