【问题标题】:function is not triggered while dragging DIV拖动DIV时不触发功能
【发布时间】:2019-02-28 11:58:16
【问题描述】:

我有 .main-container DIV,里面有 2 个子 DIV,我想让这些 DIV 可拖动。

我正在使用 jquery-ui 来实现这一点。如果我编写以下代码,我可以拖动:

index.js

$( function() {
    $( ".text1" ).draggable({containment: 'parent'});
  } )


  $( function() {
    $( ".text2" ).draggable({containment: 'parent'});
  } )

现在我想让上面的代码动态化,即我想创建一个接收类名作为参数的函数。

像这样:

function drag(val){
    console.log("here", val)
        $(val).draggable({containment: 'parent'});

  }

但是,当我创建一个函数时,它不起作用。有人告诉我这里出了什么问题。

index.html

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>New POC</title>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <link rel="stylesheet" href="./index.css">
</head>
<body>
    <div class="main-container">
        <div class="text1" ondrag="drag('.test1')">Text 1</div>
        <div class="text2" ondrag="drag('.test2')">Text 2</div>
    </div>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"
    integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
    crossorigin="anonymous"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <script src="./index.js"></script>
</body>
</html>

【问题讨论】:

  • draggable="true" ondrag="drag('.test1')"

标签: javascript jquery jquery-ui jquery-ui-draggable


【解决方案1】:

您需要在标签中添加一个可拖动='true' 的附加属性:

<div class="text1" draggable='true' ondrag="drag('.test1')">Text 1</div>
<div class="text2" draggable='true' ondrag="drag('.test2')">Text 2</div>

【讨论】:

  • 谢谢.. 工作正常
猜你喜欢
  • 1970-01-01
  • 2014-05-25
  • 2020-08-27
  • 1970-01-01
  • 1970-01-01
  • 2015-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多