我编写了以下代码,该代码成功增加了svg
行的大小,但未能减小行的大小。以下是代码:
var x = function(a,s){
var line = document.getElementById('l1');
if (s > line.getAttribute(a) ) { // if s i greater
for (var i = line.getAttribute(a); i<=s;i++ ){
line.setAttribute(a, i);
}
};
if(s < line.getAttribute(a)){
for (var i = line.getAttribute(a); i<s;i-- ){
line.setAttribute(a, i);
}
};
};
x('x2', 400); // changes the width of the svg
x('x2', 40); // fail
<html>
<body>
<svg heigth="210" width="500" id="svg1" >
<line id="l1" x1="0" y1="50" x2="100" y2="50" style="stroke:rgb(250,30,0); stroke-width:2"/>
</svg>
</body>
</html>
如您所见,它可以增加宽度但不能减小宽度?
我对i<s
和中的第二个if语句感到困惑,i--
并且我认为存在所有错误(也许不是)
如果您能解释您的答案,那将非常好,这样它不仅对我有用,而且对其他人也有用。
谢谢!
那个有效
if(s < line.getAttribute(a)){
for (var i = line.getAttribute(a); i > s;i-- ){ //changes here
line.setAttribute(a, i);
}
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句