javascript - 拖放 API 不起作用

标签 javascript html css drag-and-drop draggable

我正在为我的软件工程类(class)做一个小组元素。长话短说,我正在尝试使用拖放 HTML API,但我没有任何运气。下面是我试图实现拖放的页面的 html。元素中有多个文件,因为这是一个网站,如果没有足够的信息,我会根据要求上传其他文件。在下面的 HTML 文件中,我要做的就是删除 hello <p>进入div1 <div> .

<!DOCTYPE html>
<html>
    <head>
        <title>Fundamentals of SE (Scratchat)</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
        <!-- jQuery cdn -->
        <script src="https://code.jquery.com/jquery-3.2.1.min.js"   integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="   crossorigin="anonymous"></script>
        <link rel="stylesheet" href="css/main.css">
        <script>
            function openNav() {
                document.getElementById("mySidenav").style.width = "250px";
                document.getElementById("main").style.marginLeft = "250px";
            }

            function closeNav() {
                document.getElementById("mySidenav").style.width = "0";
                document.getElementById("main").style.marginLeft= "0";
            }
            function allowDrop(ev) {
                ev.preventDefault();
            }

            function drag(ev) {
                ev.dataTransfer.setData("text", ev.target.id);
            }

            function drop(ev) {
                ev.preventDefault();
                var data = ev.dataTransfer.getData("text");
                ev.target.appendChild(document.getElementById(data));
            }
        </script>
    </head>
    <body>

        <div id="mySidenav" class="sidenav">
            <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
            <a href="index.html">Home</a>
            <a href="aboutus.html">About us</a>
            <a href="features.html">Features</a>
            <a href="#">Build</a>
            <a href="tutorial.html">Tutorial</a>
            <a href="https://scratch.mit.edu/about">About Scratch</a>
        </div>

        <div id="main">
            <span style="font-size:30px;cursor:pointer; color: #white" onclick="openNav()">&#9776;</span>
        </div>

        <div class="div1" style="height: 100px; width: 100%; border: solid 2px black" draggable="true" ondrop="drop(event)" ondragover="allowDrop(event)">

        </div>

<!--
        <div class="container">
            <div class="jumbotron" ondrop="drop(event)" ondragover="allowDrop(event)">
                <div id="scratchWindow" style="padding-top: 200px; padding-bottom: 200px;">

                </div>
            </div>
        </div>
-->

        <div class="question" draggable="true" ondragstart="drag(event)">
            <p>
                Hello
<!--            
                <label>What Question Do You Want Your Bot To Ask?</label><br>
                <textarea
                    id = "Question"
                    rows = 3
                    cols = 20>Your Question Here</textarea>
-->
            </p>
        </div>
    </body>
</html>

这是整个网站在全局范围内使用的 CSS 文件:

.jumbotron {
    background-image: url("../res/robot-customer-service.png");
    background-position: 0% 25%;
    background-size: cover;
    background-repeat: no-repeat;
    background-attachment: fixed;
    margin-top: 20px;
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

img {
    max-width: 100%;
    max-height: 100%;
}

.sidenav {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background: white;
    overflow-x: hidden;
    transition: 0.5s;
    padding-top: 60px;
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}

.sidenav a {
    padding: 8px 8px 8px 32px;
    text-decoration: none;
    font-size: 25px;
    color: #1974fc;
    display: block;
    transition: 0.3s;
}

.sidenav a:hover {
    color: #36bac3;
}

.sidenav .closebtn {
    position: absolute;
    top: 0;
    right: 25px;
    font-size: 36px;
    margin-left: 50px;
}

#main {
    transition: margin-left .5s;
    padding: 16px;
}

@media screen and (max-height: 450px) {
  .sidenav {padding-top: 15px;}
  .sidenav a {font-size: 18px;}
}

body {

    background: url("https://cdn0.tnwcdn.com/wp-content/blogs.dir/1/files/2015/04/colortheory.jpg");
}

最佳答案

问题是你正在捕获整个 div而不仅仅是 text .

给出你的段落 - <p>元安id并添加您的 draggable & ondragstart它的属性。

function openNav() {
  document.getElementById("mySidenav").style.width = "250px";
  document.getElementById("main").style.marginLeft = "250px";
}

function closeNav() {
  document.getElementById("mySidenav").style.width = "0";
  document.getElementById("main").style.marginLeft = "0";
}

function allowDrop(ev) {
  ev.preventDefault();
}

function drag(ev) {
  ev.dataTransfer.setData("text", ev.target.id);
}

function drop(ev) {
  var data = ev.dataTransfer.getData("text");
  ev.target.appendChild(document.getElementById(data));
}
<div class="div1" style="height: 100px; width: 100%; border: solid 2px black" draggable="true" ondrop="drop(event)" ondragover="allowDrop(event)">
</div>
<div class="question">
  <p id="hello" draggable="true" ondragstart="drag(event)">
    Hello
  </p>
</div>

关于javascript - 拖放 API 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46870039/

相关文章:

javascript - jQuery ajax解析JSON数据报错

html - Angular Material : Prevent layout resizing when select box value is chosen

html - Bootstrap 4 页脚到底部高度

jquery - 选择高度的 IE7 问题

javascript - 加载微调器 div 未及时渲染

javascript - 在 GridView 中单击按钮时调用客户端事件

javascript - 是否可以使用 URL 在 JS 文件中导入文件

javascript - 发送带有可选参数的表单 POST 方法 + angularJS

html - 在 CSS 三 Angular 形的两侧添加边框?

asp.net - 如何知道 ItemTemplate 的顺序位置