gpt4 book ai didi

JavaScript 便利贴无法创建新的 div 框

转载 作者:太空宇宙 更新时间:2023-11-04 16:00:46 24 4
gpt4 key购买 nike

我在网站和网络上进行了大量搜索,但似乎找不到任何帮助。我的问题是我需要创建一个函数,在按下按钮时创建一个新的 div 框,并且该 div 框需要可​​拖动和可编辑。

function makeNote(e) {

// Check the event object if the .click is on the canvas
// or a created note
if (e.eventPhase === 2) {

// Create the new comment at the corsor postition
var $newbox = $('<div class="ui-widget-content" id="newbox" style="top:' + e.pageY + 'px; left: ' + e.pageX + 'px;"><span id="close">Delete comment</span><p>Your comment:</p><textarea></textarea></div>');
$('#canvas').append($newbox);
$newbox.draggable();

}

}


function deleteNote() {
$(this).parent('#newbox').remove();
}

// wait until the dom document is loaded
jQuery(document).ready(function () {

// listen for a .click() event on the canvas element
$('#canvas').click(function (e) {
makeNote(e);
});

// Remove the note
$("#close").click(function () {
deleteNote();
});

});
html, body { 
background-color: #cccccc;
margin: 0;
padding: 0;
height: 100%;
position: relative;
}

#newbox {
position: absolute;
background-color: white;
height: 200px;
width: 200px;
box-shadow: 10px 10px 10px #888;
padding: 20px;
z-index: 1000;
}
textarea {
background: transparent;
width: 200px;
height: 180px;
border: 0;
}
#canvas {
height:auto !important;
min-height: 100%;
height:100%;
z-index: -1000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" scr="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="JavaScript.js"></script>
<link rel="stylesheet" ahref="StyleSheet1.css" />
</head>
<body>
<div id="canvas">
</div>

</body>
</html>

最佳答案

draggable 是 jquery-ui 库的一部分。不是 jquery。

添加<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>到您的代码。

function makeNote(e) {

// Check the event object if the .click is on the canvas
// or a created note
if (e.eventPhase === 2) {

// Create the new comment at the corsor postition
var $newbox = $('<div class="ui-widget-content" id="newbox" style="top:' + e.pageY + 'px; left: ' + e.pageX + 'px;"><span id="close">Delete comment</span><p>Your comment:</p><textarea></textarea></div>');
$('#canvas').append($newbox);
$newbox.draggable();

}

}


function deleteNote() {
$(this).parent('#newbox').remove();
}

// wait until the dom document is loaded
jQuery(document).ready(function () {

// listen for a .click() event on the canvas element
$('#canvas').click(function (e) {
makeNote(e);
});

// Remove the note
$("#close").click(function () {
deleteNote();
});

});
html, body { 
background-color: #cccccc;
margin: 0;
padding: 0;
height: 100%;
position: relative;
}

#newbox {
position: absolute;
background-color: white;
height: 200px;
width: 200px;
box-shadow: 10px 10px 10px #888;
padding: 20px;
z-index: 1000;
}
textarea {
background: transparent;
width: 200px;
height: 180px;
border: 0;
}
#canvas {
height:auto !important;
min-height: 100%;
height:100%;
z-index: -1000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="JavaScript.js"></script>
<link rel="stylesheet" ahref="StyleSheet1.css" />
</head>
<body>
<div id="canvas">
</div>

</body>
</html>

关于JavaScript 便利贴无法创建新的 div 框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42302309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com