English 中文(简体)
Naming "class" and "id" HTML attributes - dashes vs. underlines [closed]
原标题:
Closed. This question is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 7 years ago.

<div id="example-value"> or <div id="example_value">?

This site and Twitter use the first style. Facebook and Vimeo - the second.

Which one do you use and why?

最佳回答

Use Hyphens to ensure isolation between your HTML and JavaScript.

Why? see below.

Hyphens are valid to use in CSS and HTML but not for JavaScript Objects.

A lot of browsers register HTML Ids as global objects on the window/document object, in big projects, this can become a real pain.

For this reason, I use names with Hyphens this way the HTML ids will never conflict with my JavaScript.

Consider the following:

message.js

message = function(containerObject){
    this.htmlObject = containerObject;
};
message.prototype.write = function(text){
    this.htmlObject.innerHTML+=text;
};

html

<body>
    <span id= message ></span>
</body>
<script>
    var objectContainer = {};
    if(typeof message ==  undefined ){
        var asyncScript = document.createElement( script );
        asyncScript.onload = function(){
            objectContainer.messageClass = new message(document.getElementById( message ));
            objectContainer.messageClass.write( loaded );
        }
        asyncScript.src =  message.js ;
        document.appendChild(asyncScript);
    }else{
        objectContainer.messageClass = new message(document.getElementById( message ));
        objectContainer.messageClass.write( loaded );
    }
</script>

If the browser registers HTML ids as global objects the above will fail because the message is not undefined and it will try to create an instance of the HTML object. By making sure an HTML id has a hyphen in the name prevents conflicts like the one below:

message.js

message = function(containerObject){
    this.htmlObject = containerObject;
};
message.prototype.write = function(text){
    this.htmlObject.innerHTML+=text;
};

html

<body>
    <span id= message-text ></span>
</body>
<script>
    var objectContainer = {};
    if(typeof message ==  undefined ){
        var asyncScript = document.createElement( script );
        asyncScript.onload = function(){
            objectContainer.messageClass = new message(document.getElementById( message-text ));
            objectContainer.messageClass.write( loaded );
        }
        asyncScript.src =  message.js ;
        document.appendChild(asyncScript);
    }else{
        objectContainer.messageClass = new message(document.getElementById( message-text ));
        objectContainer.messageClass.write( loaded );
    }
</script>

Of course, you could use messageText or message_text but this doesn t solve the problem and you could run into the same issue later where you would accidentally access an HTML Object instead of a JavaScript one

One remark, you can still access the HTML objects through the (for example) window object by using window[ message-text ];

问题回答

I would recommend the Google HTML/CSS Style Guide

It specifically states:

Separate words in ID and class names by a hyphen. Do not concatenate words and abbreviations in selectors by any characters (including none at all) other than hyphens, in order to improve understanding and scannability.

/* Not recommended: does not separate the words “demo” and “image” */
.demoimage {}

/* Not recommended: uses underscore instead of hyphen */
.error_status {}

/* Recommended */
#video-id {}
.ads-sample {}

It really comes down to preference, but what will sway you in a particular direction might be the editor you code with. For instance, the auto-complete feature of TextMate stops at a hyphen, but sees words separated by an underscore as a single word. So class names and ids with the_post work better than the-post when using its auto-complete feature (Esc).

I believe this is entirely up to the programmer. You could use camelCase too if you wanted (but I think that would look awkward.)

I personally prefer the hyphen, because it is quicker to type on my keyboard. So I would say that you should go with what you are most comfortable with, since both your examples are widely used.

Either example is perfectly valid, you can even throw into the mix ":" or "." as separators according to the w3c spec. I personally use "_" if it is a two word name just because of its similarity to space.

I use the first one (one-two) because its more readable. For images though I prefer the underscore (btn_more.png). Camel Case (oneTwo) is another option.

Actually some external frameworks (javascript, php) have difficulties (bugs?) with using the hypen in id names. I use underscore (so does 960grid) and all works great.

I would suggest underscore mainly for the reason of a javascript side-effect I m encountering.

If you were to type the code below into your location bar, you would get an error: example-value is undefined. If the div were named with underscores, it would work.

javascript:alert(example-value.currentStyle.hasLayout);




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签