我正在尝试创建一个拖放界面,以拖动按钮、文本区域、输入字段等元素。
有人会看看我的代码,看看出了什么问题吗?(我对 JQuery 很陌生)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="generator" content="CoffeeCup HTML Editor (www.coffeecup.com)">
<meta name="dcterms.created" content="Mon, 12 Aug 2013 11:49:18 GMT">
<meta name="description" content="">
<meta name="keywords" content="">
<title></title>
<!--[if IE]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link href="http://lazybots.com/bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<link rel="stylesheet" href="/resources/demos/style.css" />
<style>
.draggable { display:inline-block; position:absolute; background-color:#00FFFF; padding- right:5px;}
#draggable { cursor: n-resize; }
#containment-wrapper { margin:5px; width: 98%; height:300px; border:2px dotted #ccc; }
input[type=text] {margin:2px;}
input[type=button] {margin:2px; }
</style>
</head>
<body>
<center>
<div id="tabs">
<ul id="tablink"></ul>
</div>
<!-- ################## START OPTIONS ############### -->
<table style="width:95%;">
<th>UI Text Box</th>
<th>UI Block Text</th>
<th>UI Button</th>
<tr>
<td>
<input type="text" id="text-box-variable" placeholder="#Variable" /><br>
<input type="text" id="text-box-placeholder" placeholder="Placeholder" /><br>
<input type="text" id="text-box-width" placeholder="Width (px)" style="width:80px;" />
<input type="button" id="New_Text_Box" class="New_Text_Box btn btn-primary" value="Create Text Box" />
</td>
<td>
<input type="text" id="text-area-variable" placeholder="#Variable" /><br>
<input type="text" id="text-area-placeholder" placeholder="Placeholder" /><br>
<input type="button" id="New_Text_Area" class="New_Text_Area btn btn-primary" value="Create Text Area" />
</td>
<td>
<input type="text" id="button-define" placeholder="Define Command Name" /><br>
<input type="text" id="button-value" placeholder="Button Value" /><br>
<input type="button" id="New_Button" class="New_Button btn btn-primary" value="Create Text Button" />
<input type="button" id="New_Tab" class="New_Tab btn btn-primary" value="Create New Tab" />
</td>
</tr>
</table>
<input type="text" name="qty" value="0" id="qty" />
</center>
</body>
<script>
<!-- ############ CREATE TEXT BOX ############## -->
$(function() {
$( ".draggable" ).draggable({containment:"#containment-wrapper"});
$('.New_Text_Box').click(function(){
var width = $("#text-box-width").val();
var variable = $("#text-box-variable").val();
var placeholder = $("#text-box-placeholder").val();
var htmlData='<div class="draggable" ><input type="text" style="width:'+width+'px;" variable="'+variable+'" placeholder="'+placeholder+'" /></div>';
$('.UI').append(htmlData);
$( ".draggable" ).draggable({containment:"#containment-wrapper"});
$(".draggable").on('dblclick',function(){
$(this).draggable('destroy');
//in case you like to use if after
//$(this).hide();
//as in your code
$(this).remove();
})
});
});
<!-- ############ CREATE TEXT AREA ############## -->
$(function() {
$( ".draggable" ).draggable({containment:"#containment-wrapper"});
$('.New_Text_Area').click(function(){
var variable = $("#text-area-variable").val();
var placeholder = $("#text-area-placeholder").val();
var htmlData='<div class="draggable" ><textarea placeholder="'+placeholder+'" variable="'+variable+'"></textarea></div> ';
$('.UI').append(htmlData);
$( ".draggable" ).draggable({containment:"#containment-wrapper"});
$(".draggable").on('dblclick',function(){
$(this).draggable('destroy');
//in case you like to use if after
//$(this).hide();
//as in your code
$(this).remove();
})
});
});
<!-- ############ CREATE BUTTON ############## -->
$(function() {
$( ".draggable" ).draggable({containment:"#containment-wrapper"});
$('.New_Button').click(function(){
var define = $("#button-define").val();
var value = $("#button-value").val();
var htmlData='<div class="draggable" ><input type="button" value="'+value+'" class="btn btn-primary" onclick="ubot.runScript(\''+define+'()\');" /></div> ';
$('.UI').append(htmlData);
$( ".draggable" ).draggable({containment:"#containment-wrapper"});
$(".draggable").on('dblclick',function(){
$(this).draggable('destroy');
//in case you like to use if after
//$(this).hide();
//as in your code
$(this).remove();
})
});
});
</script>
<!-- ############ CREATE NEW TAB ############## -->
<script>
var incrementVar = 0;
$(function() {
$(".New_Tab").click(function() {
var value = parseInt($(":text[name='qty']").val()) + 1;
$(":text[name='qty']").val(value);
incrementVar = incrementVar + value;
var newTabLabel='<li><a href="#tabs-'+value+'">Tab '+value+'</a></li>';
var newTabContent='<div id="tabs-'+value+'" ><div id="containment-wrapper"> <div class="UI"></div></div> </div>';
$('#tablink').append(newTabLabel);
$('#tabs').append(newTabContent);
$( "#tabs" ).tabs();
$( "#tablink" ).tabs();
});
});
</script>
</html>
卡尔干杯:-)