我想svg從一個“物件”添加一個路徑。
myPath()for 屬性d作業正常,但我想從 object 觸發我的函式obj,而obj.action()ford不起作用......為什么?
var obj = {
action: function() {
myPath();
}
}
function myPath() {
return "M 10 10 L 50 10 L 50 90 L 10 90 Z";
}
$("button").on('click', function() {
// doesn't work... :-(
$("svg").find("path").attr("d", obj.action());
// ... but it's working with this :
//$("svg").find("path").attr("d", myPath());
});
svg {
width: 100px;
height: 100px;
background-color: #C0C0C0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>Click Me</button>
<svg><path /></svg>
uj5u.com熱心網友回復:
解決 :
var obj = {
action : function() { return myPath() }
// action : myPath //other solution
// action : () => myPath() //other solution
}
function myPath() {
return "M 10 10 L 50 10 L 50 90 L 10 90 Z";
}
$("button").on('click', function() {
$("svg").find("path").attr("d", obj.action());
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/410810.html
標籤:
上一篇:將SVG嵌入Inkscape
下一篇:SVG背景在懸停時閃爍一次
