你好吗?
我不确定一旦你点击节点或者请求结束时你想这样做,所以请采取以下代码并使用你需要的。
注意我使用.call方法,它允许您更改函数的范围(并将参数传递给它)。
的使用基本上是
(function(){ console.log(this); console.log(arguments); }).call({ obj: "Hi, i'm the scope"},1,2,3,4,5,6);
运行它萤火看看它做什么!
因此,该功能将类似于:
node.on("click", function(d) {
//In this context, 'this' is the element that got clicked
$(this).css({ background: 'red' }); //Changes to red when clicked
$.get("/user_info/" + d.name, (function(data){
$(this).css({ background: 'blue' }); //Changes to blue when finished
$("#user").html(data);
}).call(this)); //'this' in this context refers to the node that was clicked, so we pass it as a scope for the anonymous function that will handle your request
});
干杯!
谢谢,但上面的代码没有工作。我认为这必须通过使用d3来选择和更改节点属性来完成(例如,不用css hack) – eli 2012-03-24 16:12:28
我的不好,我虽然主要问题是范围,但不是实际的功能来改变颜色:) – fsodano 2012-03-24 16:15:08