最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught TypeError: n.apply is not a function - what could be the issue? - Stack Overflow

programmeradmin1浏览0评论

Trying to access the parent of a node in d3 tree layout my code looks like this

Here is the function supposed to display the parent name which it accepts as parameter

function draw(a){
  console.log(a);

  }

Here is the function that calls it in the node's mouseover event, see the last line

node.append("circle")
    .attr("class", function (d) {
        var value; 
        if(d.depth == 1){value = "marker";}
        else{value = "else";}
        return value;}
    )
    .attr("r", function(d){return d.depth == 1?d.children.length:3})
    .style("fill", function (d) {
        return d.depth>1?colors(d.parent.name):colors(d.name);})
    .style("stroke", "none")
   .on("mouseover", "draw(function(d){return d.parent.name;})");

However instead I get the error above. Any takers?

Trying to access the parent of a node in d3 tree layout my code looks like this

Here is the function supposed to display the parent name which it accepts as parameter

function draw(a){
  console.log(a);

  }

Here is the function that calls it in the node's mouseover event, see the last line

node.append("circle")
    .attr("class", function (d) {
        var value; 
        if(d.depth == 1){value = "marker";}
        else{value = "else";}
        return value;}
    )
    .attr("r", function(d){return d.depth == 1?d.children.length:3})
    .style("fill", function (d) {
        return d.depth>1?colors(d.parent.name):colors(d.name);})
    .style("stroke", "none")
   .on("mouseover", "draw(function(d){return d.parent.name;})");

However instead I get the error above. Any takers?

Share Improve this question asked Nov 8, 2015 at 10:15 user2964988user2964988 311 gold badge2 silver badges9 bronze badges 3
  • 1 Quotes?? .on("mouseover", "draw(function(d){return d.parent.name;})");??? – Tushar Commented Nov 8, 2015 at 10:16
  • Didn't get why are you writing a function in side the draw(function(d){return d.parent.name;}) is there a good reason for this... – Cyril Cherian Commented Nov 8, 2015 at 10:23
  • Sorry, not sure how you mean. So far in d3 its how I am able to access the current element, see first bracket where I set the circle class attribute – user2964988 Commented Nov 8, 2015 at 10:54
Add a ment  | 

2 Answers 2

Reset to default 3

It looks like an issue with your "mouseover" handler. You are passing a string instead of a function, which works only in the onmouseover DOM element attribute (and even there it's bad practice with confusing behavior). Try instead

on("mouseover", function (d) {
    draw(d.parent.name);
});

You'll want to do something like this:

.on("mouseover", function(d) {
    draw(d.parent.name);
});
发布评论

评论列表(0)

  1. 暂无评论