即使我没有解析页面,在页面上多次使用相同的 id 也是不好的做法吗? [英] Is using the same id multiple times on a page bad practice even when I am not parsing through the page?

查看:12
本文介绍了即使我没有解析页面,在页面上多次使用相同的 id 也是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在页面中多次使用相同的 id 会创建格式错误的 html.使用 jquery 选择器时,只会返回带有 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.

我有一个项目视图列表,它们都有一个我需要引用的元素.由于每个项目只能访问它自己的 $el,因此传入一个 id 选择器不会产生任何冲突(即使页面上有多个).

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.

在这个应用程序中,你很可能最终不得不使用 jQuery,然后你就会遇到麻烦.

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

以相同的方式为多个元素设置样式的解决方案是使用 CSS 类 - 绝对没有理由不这样做,因为它就像使用 id 一样简单.

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天全站免登陆