向div添加元素的功能不起作用。 [英] Function for adding an element to div does not work.
问题描述
我在Javascript中编写了一个简单的应用程序,它将图像或文本插入到div中,用户可以使用div中的图像或文本进行拖动。从某个时刻开始,按钮Pridat(英文Add)什么也没做,当我调试它时,它没有出错。这是整个HTML文件,复制它,创建css并尝试修复它。我看不到任何错误。我甚至评论了错误所在的行,但是我还没找到。
你能修复我的Javascript简单函数并告诉我哪里出错了吗?
非常感谢!
Pepin z Hane
Hi,
I have written a simple application in Javascript, that inserts images or text into a div and user can drag around with the images or text in the div. From some moment, the button "Pridat" (in english "Add") was doing nothing and when I debugged it, it threw no error. Here is the whole HTML file, copy it, create css and try to fix it. I can''t see any error. I even commented the lines where the error could be, but I haven''t found it.
Can you fix my Javascript simple function and tell me where is an error?
Thanks a lot!
Pepin z Hane
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Designer</title>
<link rel="Stylesheet" href="StyleSheet.css" type="text/css" media="screen"/>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.js "></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js "></script>
<script type="text/javascript">
$(function () {
$(".draggable").draggable({ containment: "parent" });
});
</script>
<script type="text/javascript">
function AddElement() {
var path = $("#ListBox1").val(); //value of the selected item
var ImgID = "Img" + $("#Counter").text(); //gets a value of the label with the id Counter and saves it into the ImgID, which is used as a unique id for each object.
var count = parseInt($("#Counter").text()) + 1; //increments Counters value for another object.
$("#Counter").text(count); //saves incremented value
if ($("#Textbox1").val().toString() == "") {
//if user has not write any text, insert to screen image.
$("#divStyle").append("<div class=\"draggable\" id=\"" + ImgID + "\"><img class=\"img\" src=\"" + path.toString() + "\"></div>");
}
else {
//else insert Text
$("#divStyle").append("<div class=\"draggable\" id=\"" + ImgID + "\"><p class=\"text\">" + $("Textbox1").val() + "</p></div>");
}
$(".draggable").draggable({ containment: "parent" }); //call draggable for dragging the elements (included the new element) by user.
}
</script>
</head>
<body>
<table>
<tr>
<td>
<form name="form1" action="Index.htm">
<select id="ListBox1" name="lbItems" multiple="multiple">
<option value="/Images/Devotion.bmp">Devotion</option>
<option value="/Images/Devotion3.bmp">Devotion3</option>
<option value="/Images/DSK.jpg">DSK</option>
<option value="/Images/HC Oceláři Třinec.png">HC Oceláři Třinec</option>
<option value="/Images/Logo_Auto_Skoda.jpg">Škoda Auto</option>
</select>
<br/>
<input name="Textbox1" id="Textbox1" type="text" value=""/>
<br />
<button class="styleButton" onclick="AddElement()">Přidat</button>
<label id="lb1">vybráno</label>
<br/>
<button class="styleButton" onclick="PrintElementProperties()">Vypiš</button>
</form>
</td>
<td>
</td>
<td>
<div id="platno">
<p class="draggable" style="height:75px; width:100px;">Drag me around</p>
</div>
</td>
</tr>
<tr>
<td>
<label id="Counter">0</label>
</td>
<td>
</td>
<td>
<div id="vypis">
</div>
</td>
</tr>
</table>
</body>
</html>
推荐答案
(function(){
(function () {
(。draggable)。draggable({containment:parent});
});
< / script >
< script type = text / javascript >
function AddElement(){
var path =
(".draggable").draggable({ containment: "parent" }); }); </script> <script type="text/javascript"> function AddElement() { var path =
(# ListBox1\" 中)VAL(); //所选项目的价值
var ImgID =Img+
("#ListBox1").val(); //value of the selected item var ImgID = "Img" +
这篇关于向div添加元素的功能不起作用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!