使用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 timesIdea: - 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
最满意答案
如果属性中有空值,那就没那么糟了。 让我们考虑这个例子:
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 theCommonList.ItemList()
method. I extend the previous example and add thenull
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 theFirst
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 };
相关问答
更多-
处理元素列表时处理NullReferenceException(Handle NullReferenceException when processing a list of elements)[2023-07-12]
如果属性中有空值,那就没那么糟了。 让我们考虑这个例子: 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 ...
-
解析XML:变量元素的NullReferenceException(Parsing XML: NullReferenceException for Variable Elements)[2022-05-30]
尴尬! 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 []时出现NullReferenceException(NullReferenceException while handling FileSystemWatcher[])[2022-04-09]
您只分配了一个能够保存FileSystemWatcher实例但不支持实例本身的数组。 您缺少以下行(在崩溃之前): fileSysWatchers[i] = new FileSystemWatcher(); 它不是Path ,它是null ,它是FileSystemWatcher实例本身。 You have only allocated an array capable of holding FileSystemWatcher instance but not the instances themselv ...