2016-11-09 10 views
1

mousedown mouseup clickイベントにrectがあります。私はRECTのMouseUpイベントがhttps://jsfiddle.net/f0vbc94s/ そこをトリガーdosen`tクリックしたときに私のコードは次のとおりです。mouseupイベントのクリックイベントを防ぐ方法

var rect=d3.select(".text").append("rect") 
        .attr("width",1000).attr("height",1000) 
      .style("fill","#00ff00"); 
rect.on("click",function(){ 
     d3.event.preventDefault 
     d3.select(this).style("fill","#000000") 
}) 
.on("mousedown",function(){ 
     d3.select(this).style("fill","#ff0000") 
}).on("mouseup",function(){ 
     d3.event.preventDefault; 
     d3.select(this).style("fill","#00ff00") 
}) 

答えて

3

あなたclickイベントがmouseupイベントを上書きしています。 をクリックし、マウスを下に置くとmousedownイベントが発生し、 が色を変更しました。マウスを離すと、mouseupイベントが発生し、 が色を変更した後、すぐにclickイベントが発生し、 に変更されました。あなたはそれを認識できませんでした。

clickイベントにコメントすると、mouseupイベントが発生します。

は、これを試してみてください

var rect=d3.select(".text").append("rect") 
        .attr("width",1000).attr("height",1000) 
      .style("fill","#00ff00"); 
//rect.on("click",function(){ 
     //d3.event.preventDefault 
     //d3.select(this).style("fill","#000000") 
//}) 
rect.on("mousedown",function(){ 
     d3.select(this).style("fill","#ff0000") 
}).on("mouseup",function(){ 
     d3.event.preventDefault; 
     d3.select(this).style("fill","#00ff00") 
}) 
+0

が、私は他の事をするイベントをクリックする必要があります。これに対処する方法は? – Fboy

+2

色の変更など、同じ目的で 'mouseup'イベントと' click'イベントを同時に使用することはできません。あなたは別のことをすることができます。あなたの実際の目的と問題は何ですか? – Aruna

+0

mouseupは私がこのことを扱う別の場所でトリガーできます、ありがとう – Fboy

関連する問題