0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DOMイベント伝播を止める(stopPropagation)

Last updated at Posted at 2016-12-04

こんにちは。
d3.js で svg の DOMイベント伝播を止める動作例(下記)を見つけたので、動かしてみました。緑色の円の方は circle だけがマウスクリックイベントを受け取り、svg には届かないことが右側に表示されます。

stopPropagation.jpg
d3_stopPropagation.html
<!DOCTYPE html>
<style>circle {stroke: #000;}</style>
<body bgcolor="#eee">
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var svg = d3.select("body").append("svg")
    .style("float", "left")
    .attr("width", 480)
    .attr("height", 480)
    .on("click", log("click svg"));

svg.append("circle")
    .style("fill", "lightgreen")
    .attr("cx", 200)
    .attr("cy", 80)
    .attr("r", 60)
    .on("click", function() { d3.event.stopPropagation(); })
    .on("click.foo", log("click circle"))  // namespacing for events

svg.append("circle")
    .style("fill", "lightblue")
    .attr("cx", 200)
    .attr("cy", 240)
    .attr("r", 60)
    .on("click.foo", log("click circle"))

var divlog = d3.select("body").append("div")
    .style("float", "left")

function log(message) {
  return function() {
    divlog.append("p")
        .text(message)
        .style("background", "#ff0")
      .transition()
        .duration(2500)
        .style("opacity", 1e-6)
        .remove();
  };
}
</script>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?