我有一个“for”循环,它从一个XML文档中提取数据并将其添加到一些JavaScript对象中,每次循环执行时,我希望它根据从XML中检索到的属性“typ”的值运行某个函数。
当前,XML中的数据正在被成功地解析,第一个“警报”证明了这一点,您可以看到哪一个产生正确的值。
但是,“if”语句中的两个“alert”行都没有被执行,因此我无法测试应该调用的“ctyp3”函数。
我哪里做错了?
for (j=0; j<arrayIds.length; j++)
{
$(xml).find("C[ID='" + arrayIds[j] + "']").each(function(){
// pass values
questions[j] = {
typ: $(this).attr('typ'),
width: $(this).find("I").attr('wid'),
height: $(this).find("I").attr('hei'),
x: $(this).find("I").attr('x'),
y: $(this).find("I").attr('x'),
baC: $(this).find("I").attr('baC'),
boC: $(this).find("I").attr('boC'),
boW: $(this).find("I").attr('boW')
}
alert($(this).attr('typ'));
if ($(this).attr('typ') == '3')
{
ctyp3(x,y,width,height,baC);
alert('pass');
} else {
// Add here
alert('fail');
}
});
}
编辑:在您提出建议后,我对ctyp3函数进行了评论,if语句继续正确执行。
CTYP3功能如下:
function ctyp3(x,y,width,height,baC)
{
alert('alsdjkfs');
document.createElement('rect');
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.fillcolor = baC;
svgTag.appendChild(this);
}
我试图创建一个对象,然后创建一个HTML元素,这是我以前没有做过的。