简体   繁体   English

为什么当我尝试将事件变量作为参数提供给 function 时,JS 会划掉事件变量?

[英]Why does JS cross out the event variable when I try to give it to an function as an argument?

I copied this code from w3schools.我从 w3schools 复制了这段代码。 But the editor does cross out the word event.但是编辑确实划掉了事件这个词。

The code worked.该代码有效。 But I am not sure if I should change something because of the crossing out.但是我不确定我是否应该因为划掉而改变一些东西。

I want to make and application with this technique.我想用这种技术制作和应用。

Picture of the crossed out word被划掉的字的图片

<!DOCTYPE HTML>
<html>
<head>
    <style>
        #div1, #div2 {
            float: left;
            width: 100px;
            height: 35px;
            margin: 10px;
            padding: 10px;
            border: 1px solid black;
        }
    </style>
    <script>
        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>


<h2>Drag and Drop</h2>
<p>Drag the image back and forth between the two div elements.</p>

<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)">
    <img id="drag1" src="https://cdn.pixabay.com/photo/2020/12/02/01/06/chipmunk-5795916__340.jpg" draggable="true" ondragstart="drag(event)" width="336" height="69">
</div>

<div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

</body>
</html>

You'd better not use inline functions in your HTML code, use addEventListener for better code.您最好不要在 HTML 代码中使用内联函数,使用addEventListener以获得更好的代码。

 let dragged = null; const source = document.getElementById("draggable"); source.addEventListener("dragstart", (event) => { // store a ref. on the dragged elem dragged = event.target; }); const target = document.querySelectorAll(".dropzone"); target.forEach(function (item) { item.addEventListener("dragover", (event) => { // prevent default to allow drop event.preventDefault(); }); item.addEventListener("drop", (event) => { // prevent default action (open as link for some elements) event.preventDefault(); // move dragged element to the selected drop target if (event.target.className === "dropzone") { dragged.parentNode.removeChild(dragged); event.target.appendChild(dragged); } }); });
 body { /* Prevent the user selecting text in the example */ user-select: none; } #draggable { text-align: center; background: white; }.dropzone { width: 100px; height: 40px; background: lightblue; margin: 10px; padding: 10px; }
 <div class="dropzone"> <img id="draggable" src="https://cdn.pixabay.com/photo/2020/12/02/01/06/chipmunk-5795916__340.jpg" draggable="true" width="100" height="35"> </div> <div class="dropzone"></div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么当我尝试访问它的数组时,传递给第二个 function 的 object(包含一个数组)会给出一个空数组? - Why does the object (containing an array) passed to second function, give an empty array when I try to access its array? JS:进行闭包时,如果我不存储为变量,内部函数如何访问外部函数参数? - JS: When making a closure, how does the inner function access the outer function argument if I'm not storing as a variable? 在JS中,当我“ alert()”传递给函数的参数值时,它会打印出[object Object],为什么? - In JS when I 'alert()' the value of an argument passed to the function, it prints out [object Object], why? 当我可以从PHP发出请求时,为什么Ajax会给我一个交叉起源错误? - Why does Ajax give me a cross origin error when I can make the request from PHP? 在将变量声明为函数Object的新实例时,为什么IE6会出现“期望函数”错误? - Why does IE6 give a “Function expected” error when declaring a variable as an new instance of a function Object? 当我尝试存储变量时不起作用? - When I try and store a variable it does not work? 为什么在尝试清理事件处理程序时,该事件处理程序代码会中断? - Why does this event handling code for ender break when I try to clean it up some? 是什么导致此事件处理程序继续运行?为什么当我尝试添加条件时它会停止? - What causes this event handler to keep running?, and why does it halt when I try to add a condition? 为什么即使我不尝试解析,node.js 也会给我一个解析错误? - Why does node.js give me a parsing error even when I'm not trying to parse? 当我尝试在我的 react.js 组件中迭代对象数组时,为什么会出现错误? - Why does error appear when I try to iterate array of objects in my react.js component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM