2012-03-24 97 views
0

我想在单击节点时更改节点的颜色。我有一些jQuery来上点击获取并显示在<div>一些用户信息:在d3图形中单击时更改节点颜色

node.on("click", function(d) { 

    $.get("/user_info/" + d.name, function(data){ 

      $("#user").html(data); 

     }); 

我希望能够选择的节点,并改变其颜色相同的代码,如果可能的话。

谢谢

回答

0

你好吗?

我不确定一旦你点击节点或者请求结束时你想这样做,所以请采取以下代码并使用你需要的。

注意我使用.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 
}); 

干杯!

+0

谢谢,但上面的代码没有工作。我认为这必须通过使用d3来选择和更改节点属性来完成(例如,不用css hack) – eli 2012-03-24 16:12:28

+0

我的不好,我虽然主要问题是范围,但不是实际的功能来改变颜色:) – fsodano 2012-03-24 16:15:08

相关问题