jQuery ui可调整大小:调整大小不适用于动态创建的SVG元素
我有一个SVG在a中,我想拖动它并调整它的大小。当我在HTML中静态创建元素时,如下所示:jQuery ui可调整大小:调整大小不适用于动态创建的SVG元素,jquery,jquery-ui,svg,Jquery,Jquery Ui,Svg,我有一个SVG在a中,我想拖动它并调整它的大小。当我在HTML中静态创建元素时,如下所示: <!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/themes/start/jquery-ui.css"> <script type="text/
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/themes/start/jquery-ui.css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('div').draggable({
handle: 'rect'
}).resizable({
aspectRatio: 1.0
});
});
</script>
</head>
<body>
<div style="width:400px; height:400px; border:solid thin #888; padding:10px; border-radius:4px; background-color:#ccc;">
<svg xmlns="http://www.w3.org/2000/svg" version="1.0" viewBox="0 0 400 400">
<rect x="0" y="0" width="200" height="200" style="fill:#FF0000" />
</svg>
</div>
</body>
一切正常。与一起拖动并调整大小,但当我动态生成元素时,如下所示:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/themes/start/jquery-ui.css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('body').append('<div style="width:400px; height:400px; border:solid thin #888; padding:10px; border-radius:4px; background-color:#ccc;">');
var svg = document.createElementNS('http://www.w3.org/2000/svg', "svg");
svg.setAttributeNS(null, "viewbox", "0 0 400 400");
$('div').append(svg);
var square = document.createElementNS('http://www.w3.org/2000/svg', "rect");
square.setAttributeNS(null, "width", "200");
square.setAttributeNS(null, "height", "200");
square.setAttributeNS(null, "x", "0");
square.setAttributeNS(null, "y", "0");
square.setAttributeNS(null, "style", "fill:#FF0000");
$('svg').append(square);
$('div').draggable({
handle: 'rect'
}).resizable({
aspectRatio: 1.0
});
});
</script>
</head>
<body>
</body>
</html>
只有拖动才能继续对和起作用。调整大小仅适用于,根本不会更改大小
怎么了 SVG在静态情况下区分大小写,您可以使用正确的viewBox大小写。在动态情况下,您不需要
svg.setAttributeNS(null, "viewbox", "0 0 400 400");
应该是
svg.setAttributeNS(null, "viewBox", "0 0 400 400");
SVG区分大小写,在静态情况下,使用正确的viewBox大小写。在动态情况下,您不需要
svg.setAttributeNS(null, "viewbox", "0 0 400 400");
应该是
svg.setAttributeNS(null, "viewBox", "0 0 400 400");