到什么程度,我应该执行一个DOM元素的ID的独特性? [英] To what extent should I enforce a DOM element's ID's uniqueness?

查看:126
本文介绍了到什么程度,我应该执行一个DOM元素的ID的独特性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到它有两个可能的解决方案的问题。我不确定哪种解决方案更正确。

我有一个网页上面打开一个模态对话框。此对话框举办一个元素ID为福。

现在,这不是在所有自身的问题。然而,非对话框页面有多个选项卡。其中一个其他选项卡,在某些内容的内心深处,也有一个ID为foo。这会导致视奇怪,间歇性问题上遍历DOM树时,富是第一次遇到。

不幸的是,这两个值是presenting恰好在完全相同的方式相同的信息。唯一的区别是,一个被托管的对话的内部。同样的模型被使用 - 因而是由我的视图模型生成相同的ID

我可以做两件事情之一:


  • 显式覆盖的这些ID-创建方法之一,以确保相对于该非对话时的对话框中所产生的ID是唯一的。

  • 修改我的jQuery搜索选择只从对话框的形式向内看的 - 确保对话框不会捕捉意想不到的DOM元素

这似乎就像第二个想法是一个更好的做法。我遍历少DOM节点,并且没有破解的东西。但是,一个不知情的开发,人们仍然可以通过意外不启动从正确的区域中搜索再介绍这个问题。

同样的问题可以到任何开发人员在我们的应用程序的工作可以说,虽然。他们将需要明确知道覆盖ID-创建视图模型,使得它不会产生相同的ID。

是以往任何时候都可以接受的重复的ID,如果他们封装在不同的形式?哪里的ID是唯一停火的期望是有道理的?

该ID是通过MVC的EditorFor HTML辅助生成的。我可以在它生成一种特殊形式-ID这样的方式重写这一点,但是这显然不是MVC的意图。

 <%= Html.EditorFor(型号=> model.CustomerDisplayName)%GT;

更新:看来我应该执行的ID唯一性。但是,ASP.NET MVC3似乎没有提供确保的ID一种简单的方法是独特的DOM。我可以追加对形式的ID,但是这将产生巨大的ID ..不知道这是最好的呼叫。有什么想法?


解决方案

  

是以往任何时候都可以接受的,如果他们封装在重复的ID
  不同形式的?


这是绝对不能接受,一个ID应该是唯一的。否则,你的文件是无效的。


  

在哪里的ID是唯一停火的期望是有道理的?


一个ID是唯一的的期待绝不会不再有意义。这是规范的一部分,让每个元素的唯一的ID。

I'm experiencing an issue which has two possible solutions. I am unsure which solution is 'more correct.'

I have a modal dialog which opens above a page. This dialog hosts an element with ID "foo".

Now, that is not an issue at all on its own. However, the non-dialog page has multiple tabs. One of these other tabs, deep down in some content, also has an ID "foo." This causes weird, intermittent issues depending on which "foo" is encountered first when traversing the DOM tree.

Unfortunately, both values are presenting exactly the same information in exactly the same way. The only difference is that one is being hosted inside of a dialog. The same model is being used -- thus the same ID is being generated by my ViewModel.

I can do one of two things:

  • Explicitly override one of these ID-creation methods to ensure that the ID generated in the dialog is unique when compared to that of the non-dialog.
  • Modify my jQuery search selector to only read from the dialog's form inward -- ensuring that the dialog will not capture unexpected DOM elements.

It would seem like the second idea is a better practice. I traverse less DOM nodes and don't have to hack anything in. However, to an unknowing developer, one could still accidentally re-introduce this issue by not starting searching from the proper area.

The same issue could be said to any developer working on our application, though. They would need to know explicitly to override the ID-creation of the ViewModel such that it does not create the same ID.

Is it ever acceptable to have repeating IDs if they're encapsulated in different forms? Where does the expectation of an ID being unique cease to make sense?

The ID is generated through MVC's EditorFor HTML Helper. I can override this in such a way that it generates a form-specific ID, but that was clearly not MVC's intention.

<%= Html.EditorFor(model => model.CustomerDisplayName) %>

UPDATE: It appears I should be enforcing the IDs uniqueness. However, ASP.NET MVC3 does not seem to provide an easy way of ensuring an ID is unique to the DOM. I can append on the form's ID, but that would generate huge IDs.. not sure if that is the best call. Any thoughts?

解决方案

Is it ever acceptable to have repeating IDs if they're encapsulated in different forms?

It is never acceptable, an ID should be unique. Otherwise your document is not valid.

Where does the expectation of an ID being unique cease to make sense?

The expectation of an ID being unique never ceases to make sense. It's part of the specs to have an unique ID per element.

这篇关于到什么程度,我应该执行一个DOM元素的ID的独特性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆