不确定为什么jQuery不能获取变量信息


Not sure why jQuery isn't getting the variable information

值似乎没有出来,并显示在页面上。它应该创建div,弹出的google_color和背景设置为十六进制值。

应用程序应该采取像素图像数据,并将其匹配到我的swatch库称为formatted_colors.js,这是一个数组。数组看起来像这样:

var colors = []; colors["000000"] = "black"; colors["100000"] = "black"; colors["200000"] = "black";

也许我不应该使用。each函数?虽然它是一个循环。

下面是一个片段:

<div class="rounded_color" hex="<?php echo $css_color ?>" xy="<?php echo $x.$y ?>"></div>
<div id="<?php echo $x.$y ?>"></div>
<script type="text/javascript" src="jquery-1.6.2.js"></script>
<script src="formatted_colors.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
//iterate through pixels and match rounded color to array in formatted_colors.js
$(document).ready(function() {
    $(".rounded_color").each(function(){ 
            var google_color = getColor($(this).attr("hex")); // finds a match of "hex" value in formatted_colors.js and return name to google_color
            $('#'+$(this).attr("hex")).html(google_color); // set the div's html to name which is google_color
            alert('#'+$(this).attr("id")); //testing if value is there, but shows #undefined
            $('#'+$(this).attr("hex")).css('background-color:', google_color); 
    })

// get name of color function from formatted_colors.js
function getColor(target_color){
    if (colors[target_color] == undefined) { // not found
      return "no match";
    } else {
      return colors[target_color];
    }
  } // end getColor function

}) // end ready function
</script>

对不起,我是新手,所以我不知道现在该怎么做。

下面是我的全部代码:http://pastebin.com/HEB3TWZP

提前感谢!

您不需要连接#this是迭代中的当前元素。

你也可能想做一些像var $this = $(this);清理你的代码,你不是在同一迭代内一遍又一遍地重新创建jQuery对象。