我在编写一个jQuery插件时写了下面的代码来制作标签云。我以格式[{tag:“028”,count:15},{tag:“101”,count:357}]传递了一组数据。我现在将云创建为跨度,根据计数规格化大小。跨度是正确创建的,即它们具有正确的大小和文本。我添加了一些事件,点击和鼠标事件。无论点击哪个范围,它总是向我显示阵列中最后一个元素的警报。一旦代码被提取到一个单独的函数,为什么JavaScript代码内循环工作?
在尝试调试正在发生的事情时,我将元素创建代码提取为单独的函数。一旦我这样做了,点击事件就能正常工作,即点击事件显示正确的数据点击的是什么范围。
我假定两个版本都会产生相同的结果。为什么click事件一旦将元素创建提取到它自己的函数中就会工作?
这是一个没有工作的版本:
for (var i = 0; i < tagList.length; ++i) {
if (tagList[i] != null) {
var tagValue = tagList[i].tag;
var tagCount = tagList[i].count;
var size = getNormalizedSize(tagCount);
var theSpan = getText(tagValue, tagCount); // <span style="font-size: {1}em">{0}<\/span>
var theAlert = getAlert(tagValue, tagCount); // "Project {0} is has logged in {1} drawings"
var newElement = $(theSpan);
newElement.click(function() {
alert(theAlert); // Always shows data from last element in array
}).mouseenter(function(event) {
$(this).css('backgroundColor', '#FFC');
}).mouseleave(function() {
$(this).css('backgroundColor', '#FFF');
});
this.append(newElement).append(" ");
}
}
这是工作的版本:
for (var i = 0; i < tagList.length; ++i) {
if (tagList[i] != null) {
var tagValue = tagList[i].tag;
var tagCount = tagList[i].count;
var tagElem = buildElement(tagValue, tagCount);
this.append(tagElem).append(" ");
}
}
function buildElement(tagValue, tagCount) {
var size = getNormalizedSize(tagCount);
var theSpan = getText(tagValue, tagCount); // <span style="font-size: {1}em">{0}<\/span>
var theAlert = getAlert(tagValue, tagCount); // "Project {0} is has logged in {1} drawings"
var newElement = $(theSpan);
newElement.click(function() {
alert(theAlert);
}).mouseenter(function(event) {
$(this).css('backgroundColor', '#FFC');
}).mouseleave(function() {
$(this).css('backgroundColor', '#FFF');
});
return newElement;
}
谢谢,我错过了那个关键。每天学习新东西,特别是在这个网站上。 – xecaps12 2011-04-05 15:04:40