简体   繁体   English

不知道为什么 jQuery 没有得到变量信息

[英]Not sure why jQuery isn't getting the variable information

The values don't seem to be coming out and showing up on the page.这些值似乎没有出现并显示在页面上。 It should be creating divs that pop up with the google_color and the background set to the hex value.它应该创建带有google_color和背景设置为十六进制值的 div。

The app is suppose to take pixel image data and match it to my swatch library known as formatted_colors.js, which is an array.该应用程序假设获取像素图像数据并将其与我的名为 formatted_colors.js 的样本库相匹配,该库是一个数组。 The array looks like this:数组如下所示:

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

Maybe I'm not suppose to use the.each function?也许我不应该使用.each function? Although it is a loop.虽然它是一个循环。

Here is a snippet:这是一个片段:

<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>

Sorry, I'm new to this so I'm not sure what to do exactly now.抱歉,我是新手,所以我不确定现在该怎么做。

Here is my entire code: http://pastebin.com/HEB3TWZP这是我的完整代码: http://pastebin.com/HEB3TWZP

Thanks in advance!提前致谢!

You don't need to concatenate # .您不需要连接# this is the current element in the iteration. this是迭代中的当前元素。

Also you might want to do something like var $this = $(this);此外,您可能想做类似var $this = $(this); Cleans up your code and you aren't recreating the jQuery object over and over again within the same iteration.清理您的代码,您不会在同一迭代中一遍又一遍地重新创建 jQuery object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM