简体   繁体   中英

how to get svg element type

i have a question, how can i get the type of svg element, btw i use d3.js

i have sth like this

var selectedElement = svg.select("." + STYLE.selected);

         if (selectedElement instanceof SVGCircleElement){
            alert("here circle");
            selectedElement.style("fill", function(){return d3.rgb(d3.select(this).attr("fill"));});
         }
           if (selectedElement instanceof SVGPathElement){
             alert("here path");
                appendMarkerm(selectedElement,false);

         }

but it seems didnt work , can someone help out here ,thanks !!


***finally, i made it work like this*** 

var selectedElement = svg.select("." + STYLE.selected);
           if (selectedElement.node() instanceof SVGCircleElement){
            selectedElement.style("fill", function(){return d3.rgb(d3.select(this).attr("fill"));});
         }
           if (selectedElement.node() instanceof SVGPathElement){
            changeMarkerStyle(selectedElement,false); 
         }

cauz selection.node() will return the first element of the selection

Just use the tagName property:

svg.select("." + STYLE.selected)
   .call( function(){

      switch( selectedElement.tagName.toLowerCase() ) {
        case 'circle': 
          alert("here circle");
          this.style("fill", function(){return d3.rgb(d3.select(this).attr("fill"));});
          break;

        case 'path':
          alert("here path");
          appendMarkerm( this, false );
          break;
      }

    });

EDIT

d3js' select() does not return the element itself, but a d3js wrapper for it (very much like in jQuery, eg). So the easiest way, would be to use the call() method to apply a function to all matches (in the case of just select() this is just one).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM