获取包含在另一个DIV中被拖放的DIV元素内的Span元素的文本。

huangapple go评论78阅读模式
英文:

Get the text of a Span element contained inside a DIV element - which was drag-dropped into another Div

问题

以下是您要翻译的内容:

我有一个可拖动的DIV元素,里面有一个带有一些文本的span元素。

<div draggable="true" style="border:5px solid blue; height:30px; width:150px;">
    <span>Value123</span>
</div>

这个DIV没有任何ID或类名。我不允许在这个DIV中添加任何属性

我将这个DIV拖到另一个DIV(放置区域)中。

<div id="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)" style="border: 2px solid blue; height:200px; width:400px;">

这是两个JavaScript函数。

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

function drop(ev) {
    ev.preventDefault();                     
    var data = ev.dataTransfer.getData("text");
    console.log(data); // 如何在这里获取SPAN文本????
}

在控制台中我得到了空白。

是否有一种方法可以在第一个DIV被拖到第二个DIV之后在控制台中打印出span文本?

这是完整HTML页面的代码。

<html>
<head>
    <script>
      function allowDrop(ev) {
        ev.preventDefault();
      }
      
      function drop(ev) {
          ev.preventDefault();                     
          var data = ev.dataTransfer.getData("text");
          console.log(data); // 如何在这里获取SPAN文本????
      }
    </script>
</head>
<body>
    <div draggable="true" style="border:5px solid blue; height:30px; width:150px;">
        <span>Value123</span>
    </div>
    <br/><br/>
    <div id="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)" style="border: 2px solid blue; height:200px; width:400px">        
</body>    
</html>

请注意,我已经忽略了代码部分,只返回了翻译的内容。

英文:

I have a DIV element which is draggable, inside this DIV, I have a span element with some text.

&lt;div draggable=&quot;true&quot; style=&quot;border:5px solid blue; height:30px; width:150px;&quot;&gt;
    &lt;span&gt;Value123&lt;/span&gt;
&lt;/div&gt;

This DIV does not have any ID, or Class Name. I am not allowed to add any attribute in this DIV.

I am dragging this DIV into another DIV (Drop zone).

&lt;div id=&quot;dropzone&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot; 
style=&quot;border: 2px solid blue; height:200px; width:400px&quot;&gt;

These are the two JavaScript functions.

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

function drop(ev) {
    ev.preventDefault();                     
    var data = ev.dataTransfer.getData(&quot;text&quot;);
    console.log(data); // How can I get the SPAN text here ????
}

I am getting blank in the console.

Is there any way to get the span text printed in the console after the first DIV is dragged into the second div ?

Here is the code for the full HTML Page.

&lt;html&gt;
&lt;head&gt;
    &lt;script&gt;
      function allowDrop(ev) {
        ev.preventDefault();
      }
      
      function drop(ev) {
          ev.preventDefault();                     
          var data = ev.dataTransfer.getData(&quot;text&quot;);
          console.log(data); // How can I get the SPAN text here ????
      }
    &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;div draggable=&quot;true&quot; style=&quot;border:5px solid blue; height:30px; width:150px;&quot;&gt;
        &lt;span&gt;Value123&lt;/span&gt;
    &lt;/div&gt;
    &lt;br/&gt;&lt;br/&gt;
    &lt;div id=&quot;dropzone&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot; 
    style=&quot;border: 2px solid blue; height:200px; width:400px&quot;&gt;        
&lt;/body&gt;    
a&lt;/html&gt;

答案1

得分: 1

你可以使用 draggable 属性获取文本:

工作示例:

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

function drop(ev) {
  var data = $('div[draggable=true]').find('span').text();
  console.log(data);
}
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>

<div draggable="true" style="border:5px solid blue; height:30px; width:150px;">
  <span>Value123</span>
</div>
<br/><br/>
<div id="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)" style="border: 2px solid blue; height:200px; width:400px">

对于多个可拖动的 div 元素,需要在 jQuery 代码中进行轻微的更改。

请参考此链接:https://jsfiddle.net/uoLj78xw/

英文:

You can use the draggable attribute to get the text:

Working snippet:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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



function drop(ev) {
  var data = $(&#39;div[draggable=true]&#39;).find(&#39;span&#39;).text();
  console.log(data);
}

<!-- language: lang-html -->

&lt;script src=&quot;https://code.jquery.com/jquery-3.6.0.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://code.jquery.com/ui/1.13.2/jquery-ui.js&quot;&gt;&lt;/script&gt;




&lt;div draggable=&quot;true&quot; style=&quot;border:5px solid blue; height:30px; width:150px;&quot;&gt;
  &lt;span&gt;Value123&lt;/span&gt;
&lt;/div&gt;
&lt;br/&gt;&lt;br/&gt;
&lt;div id=&quot;dropzone&quot; ondrop=&quot;drop(event)&quot; ondragover=&quot;allowDrop(event)&quot; style=&quot;border: 2px solid blue; height:200px; width:400px&quot;&gt;

<!-- end snippet -->

For multiple draggable divs a slight change in jquery code is needed.

Check this: https://jsfiddle.net/uoLj78xw/

答案2

得分: 0

body标签上添加ondragstart事件监听器。每当事件被触发时,e.target将返回实际可拖动的div。使用e.target.childNodes[1].innerHTML来获取该div的第一个子元素内部的文本。只需使用e.dataTransfer.setData('text', data);来设置数据,现在可以在drop事件监听器中读取数据。

document.body.addEventListener('dragstart', (e) => {
  const data = e.target.childNodes[1].innerHTML;
  e.dataTransfer.setData('text', data);
});

function drop(e) {
  e.target.innerHTML = e.dataTransfer.getData('text');
}

function dragOver(e) {
  e.preventDefault();
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    .d2 {
      width: 200px;
      height: 100px;
      border: 1px solid green;
    }
  </style>
  <title>Document</title>
</head>

<body>
  <div draggable="true" style='height: 50px;width: 50px;border: 1px solid red;'>
    <span>
      hii
    </span>
  </div>
  <div draggable="true" style='height: 50px;width: 50px;border: 1px solid red;'>
    <span>
      bye
    </span>
  </div>
  <div class='d2' ondragover='dragOver(event)' ondrop='drop(event)'>

  </div>

</body>

</html>
英文:

Add the ondragstart event listener on the body tag itself. Whenever the event is fired e.target will return the actual draggable div. Use e.target.childNodes[1].innerHTML to get the text inside the first child of that div. Just set the data using e.dataTransfer.setData(&#39;text&#39;, data); and now you can read the data on drop event listener.

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

document.body.addEventListener(&#39;dragstart&#39;, (e) =&gt; {
  const data = e.target.childNodes[1].innerHTML;
  e.dataTransfer.setData(&#39;text&#39;, data);
});

function drop(e) {
  e.target.innerHTML = e.dataTransfer.getData(&quot;text&quot;);
}

function dragOver(e) {
  e.preventDefault();
}

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;

&lt;head&gt;
  &lt;meta charset=&quot;UTF-8&quot;&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  &lt;style&gt;
    .d2 {
      width: 200px;
      height: 100px;
      border: 1px solid green;
    }
  &lt;/style&gt;
  &lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;div draggable=&quot;true&quot; style=&#39;height: 50px;width: 50px;border: 1px solid red;&#39;&gt;
    &lt;span&gt;
            hii
        &lt;/span&gt;
  &lt;/div&gt;
  &lt;div draggable=&quot;true&quot; style=&#39;height: 50px;width: 50px;border: 1px solid red;&#39;&gt;
    &lt;span&gt;
            bye
        &lt;/span&gt;
  &lt;/div&gt;
  &lt;div class=&#39;d2&#39; ondragover=&#39;dragOver(event)&#39; ondrop=&#39;drop(event)&#39;&gt;

  &lt;/div&gt;

&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月12日 19:19:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76456143.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定