如何将事件侦听器添加到svg中的对象?

时间:2023-01-27 19:00:19

I would like to create a web page displaying an interactive svg: since several svg may be used, the various objects displayed will have different IDs, so the event listeners (to catch a mouse click, for example) have to be dynamic.

我想创建一个显示交互式svg的网页:由于可能会使用多个svg,因此显示的各种对象将具有不同的ID,因此事件侦听器(例如,抓住鼠标单击)必须是动态的。

Starting from this snippet

从这个片段开始

var a = document.getElementById("alphasvg");
a.addEventListener("load",function(){
        var svgDoc = a.contentDocument;
        var delta = svgDoc.getElementById("delta");
        delta.addEventListener("click",function(){alert('hello world!')},false);
    },false);

I would like to find a way to cycle through all the objects of the svg (maybe having a particular class) and attach an even listener to them.

我想找到一种方法来遍历svg的所有对象(可能有一个特定的类)并为它们附加一个偶数监听器。

update

更新

So JQuery 'each' function may be a suitable option, but it seems that JQuery doesn't handle the svg DOM so well. Is there any other available option? (like a JQuery plugin?)

所以JQuery'each'函数可能是一个合适的选项,但似乎JQuery不能很好地处理svg DOM。还有其他可用选项吗? (像JQuery插件?)

1 个解决方案

#1


13  

This is my preferred structure for adding event listeners to SVG elements with vanilla js...

这是我用vanilla js向SVG元素添加事件监听器的首选结构...

// select elements
var elements = Array.from(document.querySelectorAll('svg .selector'));

// add event listeners
elements.forEach(function(el) {
   el.addEventListener("touchstart", start);
   el.addEventListener("mousedown",  start);
   el.addEventListener("touchmove",  move);
   el.addEventListener("mousemove",  move);
})

// event listener functions

function start(e){
  console.log(e);
  // just an example
}

function move(e){
  console.log(e);
  // just an example
}

The sample code you present is somewhat contrived, but here's a rewrite that makes it work...

您提供的示例代码有点人为,但这是一个重写,使其工作......

var a = document.getElementById("alphasvg");
a.addEventListener("load",function(){
  var svgDoc = a.contentDocument;
  var els = svgDoc.querySelectorAll(".myclass");
  for (var i = 0, length = els.length; i < length; i++) {
    els[i].addEventListener("click", 
       function(){ console.log("clicked"); 
    }, false);
  }
},false);

#1


13  

This is my preferred structure for adding event listeners to SVG elements with vanilla js...

这是我用vanilla js向SVG元素添加事件监听器的首选结构...

// select elements
var elements = Array.from(document.querySelectorAll('svg .selector'));

// add event listeners
elements.forEach(function(el) {
   el.addEventListener("touchstart", start);
   el.addEventListener("mousedown",  start);
   el.addEventListener("touchmove",  move);
   el.addEventListener("mousemove",  move);
})

// event listener functions

function start(e){
  console.log(e);
  // just an example
}

function move(e){
  console.log(e);
  // just an example
}

The sample code you present is somewhat contrived, but here's a rewrite that makes it work...

您提供的示例代码有点人为,但这是一个重写,使其工作......

var a = document.getElementById("alphasvg");
a.addEventListener("load",function(){
  var svgDoc = a.contentDocument;
  var els = svgDoc.querySelectorAll(".myclass");
  for (var i = 0, length = els.length; i < length; i++) {
    els[i].addEventListener("click", 
       function(){ console.log("clicked"); 
    }, false);
  }
},false);