6

質問があります。svg 要素のタイプを取得するにはどうすればよいですか。ところで、d3.js を使用しています。

私はこのようなものを持っています

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);

         }

しかし、それはうまくいかないようです、誰かがここで助けてくれます、ありがとう!!


***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() は選択の最初の要素を返します

4

1 に答える 1

5

tagNameプロパティを使用するだけです:

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;
      }

    });

編集

d3js'select()は要素自体を返すのではなく、要素の d3js ラッパーを返します (jQuery などと非常によく似ています)。したがって、最も簡単な方法は、call()メソッドを使用してすべての一致に関数を適用することです (select()これが 1 つだけの場合)。

于 2013-08-30T09:39:11.837 に答える