2015-10-14 48 views
0

在当前的init函数中,我使用Snap.load在SVG中加载,然后尝试获取SVG的子路径元素;但是,运行时,路径功能在控制台中输出“0”和“0”。超时功能已设置,但仍似乎不识别任何'路径'元素。无法获得SVG路径 - Snap.svg

我该如何去获取脚本中输出的SVG'路径'元素?以下是当前html和脚本的示例。

HTML

的SVG资产之前已加载:SVG的资产已经被加载

<div class="icon-svg-morph" id="icon-svg"> 
    <svg class="animate-object icon-svg"></svg> 
</div> 

后:

<div class="icon-svg-morph" id="icon-svg"> 
    <svg class="animate-object icon-svg" height="150" viewBox="0 0 500 500" width="150"> 
     <desc>Created with Snap</desc> 
     <defs></defs> 
     <path 
      d="M345,230l-95-95l-95,95l-55,95l150,50l150-50L345,230z M250,334l-54.5-84L141,289 
l31-52l78-75l78,75l31,52l-54.5-39L250,334z" 
      id="icon-path" 
      style="fill: #339ed9;" 
     > 
    </path> 
    </svg> 
</div> 

JS

$.svgElement.prototype = { 
    init: function() { 
     var s = Snap('.icon-svg'); 

     Snap.load('asset/img/icon.svg', function (response) { 
      var icon = response; 
      s.append(icon); 
     }); 

     s.attr({ 
      height: 150, 
      viewBox: 0 + ' ' + 0 + ' ' + 500 + ' ' + 500, 
      width: 150 
     }); 

     this.path(); 
    }, 

    path: function() { 
     var paths = $('.icon-svg').children('path'); 

     console.log(paths.length); 

     if (! paths.length) { 
      setTimeout(function(){ 
       console.log(paths.length); 
      }, 100); 

      return; 
     } 
    } 
}; 

Click to view the current example.

+0

不知道它会修复它,但至少我可以告诉你,如果超时后不更新'路径',你的超时是无用的。 – Kaiido

回答

0

如果你想要的路径,我只想用捕捉全选

s.selectAll('path') or s.select('path') 

,如果你只想要一个,你可能需要确保s是要选择访问。