javascript拖放,添加事件监听器。 -完全迷失了

标签 javascript mobile drag-and-drop addeventlistener

嘿,我不喜欢问这样的开放性问题,但是,我对这个问题很迷茫。我正在尝试找出如何专门针对触摸设备进行拖放操作。我见过的所有关于拖放的在线教程都有一些奇怪的 if(IE) 语句,这些语句使其难以阅读,而且它们甚至无法在触摸设备上运行。这让我发疯,因为这在 actionScript 中很容易做到,而且我还在学习 JS。

令我沮丧的是,我什至无法让 addEventListener 工作:

<html>
<head>
<title>Drag and Drop test</title>
<style type="text/css">

#box{
top:200px;
width:100px;
height:100px;
background:red;
position:relative;
}

#box2{
top:200px;
width:100px;
height:100px;
background:black;
position:relative;
}

</style>
</head>
<body>

<div id="box">something something something</div>

<div id="box2"></div>
<script type="text/javascript">

 function doSomething() {
  myImage.style.width = '200px';
}

var myImage = document.getElementById('box');

myImage.addEventListener('click', doSomething, false);
</script>
</body>
</html>

我实际上从另一个问题的答案中提取了 addEventListener 的代码,但它似乎不起作用。 如果这很重要的话,我正在运行 safari。谢谢

最佳答案

你肯定需要使用 JavaScript 框架。 DD 在桌面 Web 应用程序中的复杂性已经够难的了,更不用说在移动设备上了。看看YUI3's Drag and Drop 。它有某种移动支持:

Native gesture support was added to DD in 3.2.0. This support is transparent and the implementor should not have to do anything to use this functionality. When dd is used, loader will check the device to see if it contains support for Gesture Events, if the device supports these events the dd-gestures module will automatically be loaded as well as it's dependencies. At this point, DD will operate as usual but it will utilize the native gesture events instead of mouse based events.

关于javascript拖放,添加事件监听器。 -完全迷失了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6781360/

相关文章:

javascript - 使用字符串访问 JSON 或 JS 属性

javascript - 如何知道使用 map 功能时单击了哪个元素以及如何将该值传递给另一个组件?

java - 通过单击按钮创建 Imagebutton 的新实例

android - CSS android 和 iOS 问题

winapi - 如何创建未知大小的 CFSTR_FILEDESCRIPTOR?

javascript - 如何在 React 上设置具有撤消操作的初始状态?

javascript - 将点击事件传递给嵌套组件 - React | Material -UI

javascript - Highcharts 基于数据比较的动态列颜色

iphone - 我怎样才能让它在 android/iphone 上播放视频?

javascript - 如何修复多张图片的拖放问题?