即使在我不通过页面解析的情况下,在页面上使用相同的ID也会出现错误的练习? [英] Is using the same id multiple times on a page bad practice even when I am not parsing through the page?

查看:148
本文介绍了即使在我不通过页面解析的情况下,在页面上使用相同的ID也会出现错误的练习?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,在页面中多次使用相同的id会导致格式错误的html。当使用jquery选择器时,只会返回带有id的第一个元素,但在我的应用程序中,我不应该遇到此问题。



我有项目列表意见都将有一个我需要参考的元素。由于每个项目只能访问自己的 $ el ,所以传递给id选择器的东西不会产生任何冲突(即使页面上有多个)。



我在一个小提琴中模拟了我的意思这里



在这个项目中,我没有进行任何页面分析,所以我认为它应该是安全的。我有什么理由不应该这样做?

解决方案

您只知道当前的要求是什么。

最好是遵守标准和有效的代码/标记,因为你不知道未来会发生什么。



在这个应用程序中,必须使用jQuery,那么你将遇到麻烦。



以同样的方式为多个元素设计样式的解决方案是使用CSS类 - 绝对没有理由不做所以,就像使用ID一样简单。


I understand that using the same id multiple times in a page creates malformed html. When using a jquery selector only the first element with the id will be returned, but in my application I shouldn't be running into this issue.

I have list of items views that will all have an element I need to refer to. Since each item only has access to its own $el passing in an id selector for something will not produce any conflicts (even if there are multiple on the page).

I've simulated what I mean in a fiddle here

In this project I am not doing any page wide parses so I believe it should be safe. Is there any reason why I should not be doing this?

解决方案

You only know what the current requirements are.

It is best to keep to standards and to valid code/markup as you don't know what will happen in the future.

In this application, you may very well end up having to use jQuery and then you will be in trouble.

The solution for styling multiple elements the same way is to use CSS classes - there is absolutely no reason not to do so, as it is just as simple as using ids.

这篇关于即使在我不通过页面解析的情况下,在页面上使用相同的ID也会出现错误的练习?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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