2011-08-09 30 views
9

$ html aralığından $ hexcode değerleri olan ID'leri eklemeye çalışıyorum. Bunu jQuery ile nasıl yapabilirim? Sonunda, bu hexcode değerlerini yakalayıp bir renk diziniyle eşleştirmem gerekecek.jQuery - bir diziye eleman eklenmesi

<?php 
// display every color in the world 

$r = 0; 
$g = 0; 
$b = 0; 
$i = 0; 
$step = 16; 

for($b = 0; $b < 255; $b+=$step) { 
    for($g = 0; $g < 255; $g+=$step) { 
     for($r = 0; $r < 255; $r+=$step) { 
     $hexcolor = str_pad(dechex($r), 2, "0", STR_PAD_LEFT).str_pad(dechex($g), 2, "0", STR_PAD_LEFT).str_pad(dechex($b), 2, "0", STR_PAD_LEFT); 
     echo '<span class="color_cell" id="'.$hexcolor.'" style="width: 5px; height: 5px; background-color:#'.$hexcolor.'; border: 1px dotted;">&nbsp;</span>' 

     if($i%256 == 0) { 
      echo "<br />"; 
     } 
     $i++; 
     } 
    } 

} 
?> 
<script src="jquery-1.6.2.js"></script> 
<script type="text/javascript"> 

var ids = []; 

    $(document).ready(function($) {  
    $(".color_cell").bind('click', function() { 
     alert('Test'); 
     //how do i add the ID (which is the $hexcolor into this array ids[]? 
     ids.push($(this).attr('id'));  
    }); 
}); 

Şimdiden teşekkürler!

+0

bana tamam görünüyor. Ne çalışmıyor? – Phil

+0

Hangi problemi yaşıyorsunuz? Ne çalışmıyor? – jergason

cevap

23

Bunu deneyin, her bir döngü sonunda, ids dizisi tüm hex kodlarını içerir.

var ids = []; 

    $(document).ready(function($) { 
    var $div = $("<div id='hexCodes'></div>").appendTo(document.body), code; 
    $(".color_cell").each(function() { 
     code = $(this).attr('id'); 
     ids.push(code); 
     $div.append(code + "<br />"); 
    }); 



}); 
+0

Teşekkürler! Dizimi kontrol etmek/yazdırmak için kolay bir yol nedir? Chrome'da veya Firefox'ta Firebug kullanıyorsanız – Richard

+0

console.log (ids) bunu geliştirici konsoluna kaydeder. – jergason

+0

var divnew = document.createElement ('div'); Için (var i = 0; i '; } $ ('Gövde') ekle (divnew); –

2
var ids = []; 

    $(document).ready(function($) {  
    $(".color_cell").bind('click', function() { 
     alert('Test'); 

     ids.push(this.id);  
    }); 
});