首页 \ 问答 \ 使用insertBefore进行HTML5拖放(HTML5 Drag and Drop with insertBefore)

使用insertBefore进行HTML5拖放(HTML5 Drag and Drop with insertBefore)

我正在进行一个拖放项目。

方法 :我正在使用HTML5拖动事件。
代码 :请看这里的小提琴
问题 :它有时只能工作,我检查了一百万次代码

Idea:
- Get element and drag it over a div with id: LayerN (N=number)
- Create a new layer before LayerN when dropping
- AppendChild to new Layer.
- Remove empty layers if there are any.

有什么我做错了或太复杂了吗? 我不想使用JQuery框架,我想了解我在做什么。 非常感谢您的阅读,我们将非常感谢您的帮助。


I am working on a drag and drop project.

How: I am using the HTML5 drag events.
Code: See fiddle here
Problem: It only works sometimes, and I checked the code a million times

Idea:
- Get element and drag it over a div with id: LayerN (N=number)
- Create a new layer before LayerN when dropping
- AppendChild to new Layer.
- Remove empty layers if there are any.

Is there anything I am doing wrong or too complex? I don't want to use a JQuery framework, i want to understand what I am doing. Thanks a lot for reading, any help will be highly appreciated.


原文:https://stackoverflow.com/questions/19799077
更新时间:2022-06-12 08:06

最满意答案

如果属性中有空值,那就没那么糟了。 让我们考虑这个例子:

var items = from obj in "Hello".ToCharArray()
            select new
            {
                First = (string)null
            };    
var listTemp = items.ToList();

当你调用ToList方法时,它完全没问题。

可能,异常来自列表中的null元素,您可以从CommonList.ItemList()方法获得该元素。 我扩展了前面的示例,并将null元素添加到列表中:

var listTemp = items.ToList();
listTemp.Add(null);

var itemsProcessed = from item in listTemp
                     select new
                     {
                         item.First 
                     };    
var list = itemsProcessed.ToList();

在这种情况下,将抛出NullReferenceException ,因为当我们创建新元素并尝试获取First属性时,我们没有真正的元素。

如果在查询中添加where子句 ,则可以轻松解决此问题:

var itemsProcessed = from item in listTemp
                     where item != null
                     select new
                     {
                         item.First 
                     };

If you have null values in the properties, it's not so bad. Let's consider this example:

var items = from obj in "Hello".ToCharArray()
            select new
            {
                First = (string)null
            };    
var listTemp = items.ToList();

When you call the ToList method it's totally fine.

Probably, the exception comes from the null element in the list, which you get from the CommonList.ItemList() method. I extend the previous example and add the null element into the list:

var listTemp = items.ToList();
listTemp.Add(null);

var itemsProcessed = from item in listTemp
                     select new
                     {
                         item.First 
                     };    
var list = itemsProcessed.ToList();

In this case the NullReferenceException will be thrown, because when we create the new element and try to get the First property we don't have a real element.

This issue can be easily solved if you add the where clause in the query:

var itemsProcessed = from item in listTemp
                     where item != null
                     select new
                     {
                         item.First 
                     };

相关问答

更多
  • 如果属性中有空值,那就没那么糟了。 让我们考虑这个例子: var items = from obj in "Hello".ToCharArray() select new { First = (string)null }; var listTemp = items.ToList(); 当你调用ToList方法时,它完全没问题。 可能,异常来自列表中的null元素,您可以从CommonList.I ...
  • 我假设检查器没有检查传递给CookieCollection索引器的字符串的值是否相同。 我想如果你重组代码来: if (Request != null && Request.Cookies != null) { var place = Request.Cookies["place"]; if (place != null && place.Value == null) { string placeInformation = place.Value; } ...
  • 打开服务器和客户端上的跟踪(请参阅http://msdn.microsoft.com/zh-cn/library/ms733025.aspx ),然后使用SvcTraceViewer(http://msdn.microsoft.com/zh-cn/我们/库/ ms732023.aspx)。 它通常会给你一个更详细的例外。 My problem turned out to be 2 issues: My site reroutes Url's for all GET requests to lower cas ...
  • 问题是这样的: model.importacaoConfig.tipo 你没有实例化 importacaoConfig ,所以它是null(因此你得到该行的例外。 添加此行以实例化它(在两个现有行之间): model.importacaoConfig = new importacaoConfig(); 所以你的控制器将是: gedaiapp.Models.loadViewModel model = new gedaiapp.Models.loadViewModel(); model.importacaoC ...
  • 尴尬! Dweeberly的道具指出了它。 我只需要更改if语句 if(el.AreaDesc.Value!= null){} to if(el.AreaDesc!= null){} Embarassing! Props to Dweeberly for pointing it out. I just need to change the if statement from if (el.AreaDesc.Value != null){} to if (el.AreaDesc != null){}
  • 使用Collection ,它是List 的内置可自定义包装器,然后您可以覆盖InsertItem和SetItem,如果该项为null,则抛出异常。 Use Collection, it is built-in customisable wrapper for List you can then override InsertItem and SetItem and throw an exception if the item is null.
  • Bah,我讨厌找到答案后,我问了一个问题。 事实证明, List不喜欢使用GetUninitializedObject实例化,因此我不得不为List使用Activator.CreateInstance(对于大多数类不适用)添加特殊情况。 Bah, I hate finding an answer just after I asked a question about it. As it turns out List does not like to be instantiated using G ...
  • 您创建了一个新的MapTile数组,但您从不初始化各个元素。 尝试这个: res[i0 - 2, i1] = new MapTile(new Tile[tileSprites.Length]); 这将在数组的每个MapTile创建一个新的MapTile对象。 不这样做会导致您的空引用异常。 我还会考虑将MapTile的构造函数MapTile为无参数,然后像以前一样设置MapTile.sprites 。 You create a new array of MapTile but you never init ...
  • 在[HttpPost]操作方法中,您在不使用任何视图模型的情况下调用View()方法。 因此,视图内的Model属性为null。 解决方案只是调用View并传入BillViewModel 。 例如: [HttpPost] public ActionResult Index(BillViewModel billViewModel) { return View(billViewModel); } In the [HttpPost] action method, you are invoking the ...
  • 您只分配了一个能够保存FileSystemWatcher实例但不支持实例本身的数组。 您缺少以下行(在崩溃之前): fileSysWatchers[i] = new FileSystemWatcher(); 它不是Path ,它是null ,它是FileSystemWatcher实例本身。 You have only allocated an array capable of holding FileSystemWatcher instance but not the instances themselv ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。