命名Dom元素的ID属性的最佳做法 [英] Best Practice for Naming the Id Attribute of Dom Elements

查看:81
本文介绍了命名Dom元素的ID属性的最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与DOM元素的id属性的命名约定有关,我想name属性也是如此.就JavaScript而言,据我了解并做过的事情始终是使用驼峰式大小写,除了类名之外.所有课程均采用Pascal格式.

This is related to naming conventions for a DOM element's id attribute and I guess the name attribute as well. When it comes to JavaScript, from what I understand and have done is always use camel case except for the names of classes. Classes are Pascal cased.

已经说过,我主要是在ASP.NET中进行开发,在这里我遇到了id属性的命名问题.在ASP.NET中,如果将新的服务器控件拖放到页面上(这是我很少做的,我是一种类型的标记),则默认名称始终为Pascal大小写,因为它们需要符合.NET框架服务器端代码的命名准则.

Having said that I develop in ASP.NET mainly and here's where I run into a naming issue for the id attribute. In ASP.NET if you drag and drop a new server control to a page(which I rarely do, I'm a type the markup kinda guy), the default names are always Pascal cased because they need to conform to the .NET framework naming guidelines for server-side code.

因此,当要命名ASP.NET服务器控件的id属性或仅标记标记中的元素时,我遵循规则以驼峰式命名id属性(JavaScript命名准则),但这与.NET命名准则相冲突.

So when it comes to naming the id attribute of ASP.NET server controls or just elements in markup, I follow the rule to camel case the id attribute (Javascript naming guidelines), but this conflicts with the .NET naming guidelines.

那么,一,您通常为DOM元素中的id属性使用哪种大小写形式,二是在ASP.NET中开发的.NET人员如何为id属性命名?

So, one, what casing do you folks normally do for the id attribute in DOM elements and two what do the .NET folks who develop in ASP.NET do for naming the id attribute?

最重要的是,当我在标记中创建表单元素时,通常将匈牙利表示法用于文本输入,例如

On top of that when I'm creating form elements in markup, I generally use Hungarian notation for text inputs, like

<input type="text" id="txtUserName" />

或诸如此类的复选框

<input type="checkbox" id="chkSelectAll" />

这肯定与.NET服务器端代码命名准则甚至JavaScript准则背道而驰.

Which definitely goes against .NET server-side code naming guidelines and maybe JavaScript guidelines as well.

任何建议都值得赞赏.

推荐答案

  • 保留其语义.使用完整,简单的(最好是英语)单词.不要试图提出一些幻想或技术性的东西,也不要描述它是什么-我们知道这一点.描述它的作用.描述目的会增加信息价值.

    • Keep it semantic. Use complete, simple, (preferably English) words. Don't try to come up with something fancy or technical, and don't describe what it is - we know that. Describe what it does. Describing purpose adds informational value.

      请勿缩写! BW_RCB01_SW 对几年前使用我们的CSS的团队来说意义重大,但现在对我而言并没有任何意义,我必须倒退以尝试翻译 _BW_RCB01_SW 对应于我的目的,请记住该翻译或将其记录在某处.更好的? blackwhite-boxtype1-bottomleft .它更长,但也不需要Rosetta石头.

      Don't abbreviate anything! BW_RCB01_SW meant something to the team who did our CSS to years ago, but it doesn't mean anything to me now and I have to work backwards to try to translate what BW_RCB01_SW corresponds to for my purposes, and either remember that translation or document it somewhere. Better? blackwhite-boxtype1-bottomleft. It's longer but also doesn't require a Rosetta stone.

      将其全部小写,并使用下划线或连字符.我更喜欢连字符,但这完全是一种偏爱.使用连字符应该没有障碍,因为连字符在CSS或HTML中没有保留,并且ID在所有其他语言中都被视为文字字符串.小写都是经验-浪费太多时间想知道为什么这种样式不适用哦. pageContainerLeft pageContainerleft 不同.

      Keep it all lower-case and use underscores or hyphens. I prefer hyphens, but that's totally a preference. There should be no impediment to using hyphens, since they are not reserved in CSS or HTML and IDs are treated as literal strings in every other language. Lower case is all experience - too many hours wasted wondering why the heck won't this style apply oh. pageContainerLeft is not the same as pageContainerleft.

      准确地识别 该元素是什么,但仅此而已.请认真考虑您要在名称中嵌入的每条信息,以及是否有必要.在您的示例中-您是否需要 知道ID是一个复选框?不太可能.您已经知道要指代什么元素,因为它是唯一的ID,并且无论如何都必须针对该元素进行编码.

      Identify exactly what that element is, but no more. Think seriously about each piece of information you're embedding in the name and whether it's necessary. In your example - do you need to know it's a checkbox by the ID? Unlikely. You already know what element you're referring to because it's a unique ID and you have to code against that element anyway.

      这篇关于命名Dom元素的ID属性的最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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