1
我试图使用d3.js
更改具有不同技能值的每个图像。 但不知道为什么返回值无法返回图像。D3.js:如何在“if..else”语句后更改xlink:href图像
更新2016年2月5日: 似乎ignor AJAX功能
,当我看到我的开发者Tool.i得到这个HTML结果:
<g transform="translate(844,478)">
<pattern class="sNodes" id="image2" width="100%" height="100%">
<image id="node_id31" width="50" height="50"></image>
</pattern>
<circle r="25" class="sNodes" id="node_id31" fill="url(#image2)" onclick="NodesDownInlv3(31)" indicator="4-n-12-S05"></circle>
<text class="sNodes" id="node_id31" style="text-anchor:middle" x="10" y="30">4-n-12-S05</text>
</g>
图像0没有属性的图像。
这里的JS代码有关xlink:href
部分:
elemEnter.append('pattern')
.attr('class', d.class)
.attr('id', function() {
if (d.class == "bNodes") {
return 'image';
} else {
return 'image2';
}
})
.attr('width', '100%')
.attr('height', '100%')
.append('image')
.attr('xlink:href', function() {
return 'images/star_03.png'; //this return is work
var img;
if (d.class == "bNodes") {
return 'images/star_01.png'; //this is work
} else {
//return 'images/star_02.png';
//console.log(d.node_sn);
$.ajax({
url: 'php/data_Skill.php',
type: 'POST',
data: {
node_sn: parseInt(d.node_sn)
},
})
.done(function(data) {
var arr_data = data.split(";");
arr_data = arr_data.filter(function(str) {
return /\S/.test(str);
});
arr_data.forEach(function(index) {
index = index.split(',');
console.log(index);
var ind = index[0];
var skill = parseInt(index[1]); //skill = 0, 15, 20, 52, 70, 100
console.log(ind, skill);
var indicate_id = d.indicate_id.split("(");
indicate_id = indicate_id[1].split(")");
indicate_id = indicate_id[0]; //indicate_id = "4-n12-S05" ,ind="4-n-12-S05"
//console.log(mapSN)
//image = 'images/star_02.png';
if (indicate_id == ind) {
if (skill == 0) {
img = 'images/star_02.png';
} else if (skill == 15 || skill == 20 || skill == 52 || skill == 70) {
img = 'images/star_04.png';
} else if (skill == 100) {
img = 'images/star_03.png';
} else {
img = 'images/star_02.png';
}
}else{
img = 'images/star_02.png';
}
})
})
return img; //this not work
}
})
不起作用!不知道为什么该功能跳过 –
你必须先找到图像的网址,然后设置它。 Ajax需要一些时间才能获得服务器响应。你必须在函数内异步设置属性。 – Gilsha
我的代码中有一个意外错误。该属性应该已经在完成回调函数中设置。抱歉。现在就试试。 – Gilsha