2017-01-14 141 views
-1

我需要在attr方法中获取'transfrom'属性的值,但它返回undefined用jQuery .attr()方法得到svg元素<use>属性'transform'的值

my pen with full code,代码也低于并随后与来自代码创建的片段):

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<head> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"> 
</script> 
<style> 
#cont { 
    width:99vw; 
    height:99vh; 
} 
svg { 
    background-color:grey; 
    width:100%; 
    height:100%; 
} 
#point { 
    stroke:none; 
    fill:rgba(40,40,40,.9); 
    cursor:pointer; 
    } 
#crown{ 
    fill:none; 
    stroke:rgba(170,250,80,.8); 
    stroke-width:3;  
} 
</style> 
</head> 
<body> 
<div id='cont'> 
<svg xmlns:svg="http://www.w3.org/2000/svg"> 
<defs> 
    <circle cx='80' cy='100' r='25' id='point' transform='translate(0,0)'/> 
    <circle cx='80' cy='100' r='35' id='crown' transform='translate(0,0)'/> 
</defs> 
<g id='useGroup'> 
    <use xlink:href='#point' class='pointUse'/> 
    <use xlink:href='#point' class='pointUse' transform='translate(150,0)'/> 
    <use xlink:href='#crown' class='crownUse' transform='translate(150,0)'/> 
</g> 
</svg> 
</div> 
<script> 
var v = 0;  
$('.pointUse').click(function() { 
    n = $('.pointUse').attr('transform'); 
    $('.crownUse').attr('transfom', n); 
    console.log(n,'test' , v); 
    v++; 
    //console.log = undefined 
});  
</script> 
</body> 
</xml> 

$('.pointUse').click(function(e) { 
 
    n = e.currentTarget.getAttribute("transform") 
 
    crown.setAttribute('transform', n); 
 
    console.log('the attribute "transform" value of this <use/> is: ' + n); 
 
});
#cont { 
 
    width: 99vw; 
 
    height: 99vh; 
 
} 
 
svg { 
 
    background-color: grey; 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 
#point { 
 
    stroke: none; 
 
    fill: rgba(40, 40, 40, .9); 
 
    cursor: pointer; 
 
} 
 
#crown { 
 
    fill: none; 
 
    stroke: rgba(170, 250, 80, .8); 
 
    stroke-width: 3; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> 
 

 
<div id='cont'> 
 
    <svg xmlns:svg="http://www.w3.org/2000/svg"> 
 
    <defs> 
 
     <circle cx='80' cy='100' r='25' id='point' transform='translate(0,0)' /> 
 
     <circle cx='80' cy='100' r='35' id='crown' transform='translate(0,0)' /> 
 
    </defs> 
 
    <g id='useGroup'> 
 
     <use xlink:href='#point' class='pointUse' /> 
 
     <use xlink:href='#point' class='pointUse' transform='translate(150,0)' /> 
 
     <use xlink:href='#crown' class='crownUse' transform='translate(150,0)' /> 
 
    </g> 
 
    </svg> 
 
</div>

根据格式和用于规则其提示修正码已被编辑。已发布的代码已成功编译为链接的“笔”。

+0

请张贴相关的 “* [MCVE] *” 你的问题中的代码;不要指望我们去或依靠外部网站来帮助你。 –

+0

您可以使用任何您喜欢使用的符号风格,但请记住它需要可以理解。但请记住该指南的标题,它必须是“* minimal *”(代表重现问题所需的最小代码量)和“* complete *”(以准确地重现您的问题)。至于你对规则的看法,不可以:即使外部网站因任何原因失败,也可以让最广泛的受众最容易地理解你的问题。 –

+0

好吧,大卫先生,现在一切正常,我可以在stackoverflow中运行代码? –

回答

0

使用事件对象和currentTarget来获取单击的使用元素和标准DOM方法来获取/设置变换。

var v = 0; 
 
$('.pointUse').click(function(e) { 
 
    n = e.currentTarget.getAttribute("transform") 
 
    crown.setAttribute('transform', n); 
 
    console.log('the attribute "transform" value of this <use/> is: ' + n); 
 
    v++; 
 
});
#cont { 
 
    width: 99vw; 
 
    height: 99vh; 
 
} 
 
svg { 
 
    background-color: grey; 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 
#point { 
 
    stroke: none; 
 
    fill: rgba(40, 40, 40, .9); 
 
    cursor: pointer; 
 
} 
 
#crown { 
 
    fill: none; 
 
    stroke: rgba(170, 250, 80, .8); 
 
    stroke-width: 3; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> 
 

 
<div id='cont'> 
 
    <svg xmlns:svg="http://www.w3.org/2000/svg"> 
 
    <defs> 
 
     <circle cx='80' cy='100' r='25' id='point' transform='translate(0,0)' /> 
 
     <circle cx='80' cy='100' r='35' id='crown' transform='translate(0,0)' /> 
 
    </defs> 
 
    <g id='useGroup'> 
 
     <use xlink:href='#point' class='pointUse' transform='translate(0,0)'/> 
 
     <use xlink:href='#point' class='pointUse' transform='translate(150,0)' /> 
 
     <use xlink:href='#crown' class='crownUse' transform='translate(0,0)' /> 
 
    </g> 
 
    </svg> 
 
</div>

+0

伟大thx Holger将!它正在工作! 看起来像ES只需要更多关于功能的细节... –

+0

请告诉我,这是methond与jquery .animate()?或者这已经是greenSock主题了? –

+0

@AleksandrIvanitskiy我想你可能需要创建一个新的问题,如果你有一个jQuery的问题.animate()。我们两个都没有在我们的答案中使用过任何greenSock,尽管因为在你的问题中没有。 –

1

一旦我纠正代码即可获得您实际点击的对象似乎罚款(我认为这是在这里你的意图),而不是所有对象的数组。

var v = 0; 
 
$('.pointUse').click(function() { 
 
    n = $(this).attr('transform'); 
 
    console.log('the attribute "transform" value of this <use/> is: ' + n); 
 
    v++; 
 
});
#cont { 
 
    width: 99vw; 
 
    height: 99vh; 
 
} 
 
svg { 
 
    background-color: grey; 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 
#point { 
 
    stroke: none; 
 
    fill: rgba(40, 40, 40, .9); 
 
    cursor: pointer; 
 
} 
 
#crown { 
 
    fill: none; 
 
    stroke: rgba(170, 250, 80, .8); 
 
    stroke-width: 3; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> 
 

 
<div id='cont'> 
 
    <svg xmlns:svg="http://www.w3.org/2000/svg"> 
 
    <defs> 
 
     <circle cx='80' cy='100' r='25' id='point' transform='translate(0,0)' /> 
 
     <circle cx='80' cy='100' r='35' id='crown' transform='translate(0,0)' /> 
 
    </defs> 
 
    <g id='useGroup'> 
 
     <use xlink:href='#point' class='pointUse' /> 
 
     <use xlink:href='#point' class='pointUse' transform='translate(150,0)' /> 
 
     <use xlink:href='#crown' class='crownUse' transform='translate(150,0)' /> 
 
    </g> 
 
    </svg> 
 
</div>