[英]jQuery - adding elements into an array
我正在嘗試添加ID,即html span中的$ hexcode值到數組中。 我如何使用jQuery執行此操作? 最終,我將需要獲取這些十六進制值並將它們與顏色索引相匹配。
<?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;"> </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'));
});
});
提前致謝!
試試這個,在每個循環結束時,ids數組將包含所有的十六進制代碼。
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 />");
});
});
var ids = [];
$(document).ready(function($) {
$(".color_cell").bind('click', function() {
alert('Test');
ids.push(this.id);
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.