我正在尝试在 d3.js 中使用 DOM 元素的拖放来创建一个简单的形状,比如说一个圆圈,比如说一个 div。所以这就是我所做的:
<!DOCTYPE html>
<html>
<head>
<title>d3 tree with drag and drop</title>
<style type="text/css">
#dropInSVG {
width:200px;
height:200px;
margin-left:20px;
background-color:#F8F8F8 ;
}
#dropInSVG svg {
width: 200px;
height:200px;
background-color:yellow;
}
#tobeDropped{
width:50px;
height:15px;
background-color:pink;
float:left;
}
#mainContainer {
width: 250px;
height: 250px;
background-color:orange;
cursor:pointer;
}
</style>
</head>
<body>
<div id="mainContainer">
<div id="dropInSVG"></div>
<div id="tobeDropped"></div>
</div>
</body>
<script type="text/javascript" src="./lib/jquery.js"></script>
<script type="text/javascript" src="./lib/jquery-ui.js"></script>
<script type="text/javascript" src="./lib/d3.js"></script>
<script type="text/javascript" src="./lib/d3.layout.js"></script>
<script type="text/javascript" src="d3appDrag.js"></script>
</html>
JavaScript 代码:
var treeCreator = function(){};
treeCreator.prototype.callbacktest = function(svgContainer){
alert('the element has been dropped');
};
treeCreator.prototype.createTreeNode = function(theSVG){
$('#tobeDropped').remove();
theSVG.append("circle")
.style("stroke","green")
.style("fill","white")
.attr("r",40)
.attr("cx", 100)
.attr("cy", 100)
.on("mouseover", function () {
d3.select(this).style("fill", "aliceblue");
})
.on("mouseout", function () {
d3.select(this).style("fill", "red");
});
};
$(document).ready(function(){
$('#tobeDropped').draggable({containment:'#mainContainer'});
var theSVG = d3.select('#dropInSVG')
.append("svg")
.attr("width",200)
.attr("height",200);
var tc = new treeCreator();
$('#dropInSVG').droppable({
drop: function(){
tc.createTreeNode(theSVG);
}
});
});
问题是圆圈没有出现。你能看看有什么问题吗?
感谢穆罕默德·阿里