Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

html - HTML中id属性的有效值是什么?(What are valid values for the id attribute in HTML?)

为HTML元素创建id属性时,该值有哪些规则?

  ask by Mr Shark translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For HTML 4 , the answer is technically:

(对于HTML 4 ,技术上的答案是:)

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

(ID和NAME令牌必须以字母([A-Za-z])开头,后跟任意数量的字母,数字([0-9]),连字符(“-”),下划线(“ _”) ,冒号(“:”)和句点(“。”)。)

HTML 5 is even more permissive, saying only that an id must contain at least one character and may not contain any space characters.

(HTML 5更为宽容,只说一个id必须至少包含一个字符,并且不得包含任何空格字符。)

The id attribute is case sensitive in XHTML .

(在XHTML中 ,id属性区分大小写。)

As a purely practical matter, you may want to avoid certain characters.

(从纯粹的实践出发,您可能要避免使用某些字符。)

Periods, colons and '#' have special meaning in CSS selectors, so you will have to escape those characters using a backslash in CSS or a double backslash in a selector string passed to jQuery .

(在CSS选择器中,句点,冒号和'#'具有特殊含义,因此您必须在CSS中使用反斜杠或在传递给jQuery选择器字符串中使用双反斜杠来转义这些字符。)

Think about how often you will have to escape a character in your stylesheets or code before you go crazy with periods and colons in ids.

(想一想,在迷上句号和ID中的冒号之前,必须多久在样式表或代码中转义一个字符。)

For example, the HTML declaration <div id="first.name"></div> is valid.

(例如,HTML声明<div id="first.name"></div>是有效的。)

You can select that element in CSS as #first\.name and in jQuery like so: $('#first\\.name').

(您可以在CSS中选择该元素作为#first\.name ,在jQuery中选择该元素: $('#first\\.name').)

But if you forget the backslash, $('#first.name') , you will have a perfectly valid selector looking for an element with id first and also having class name .

(但是,如果你忘记反斜杠, $('#first.name')你将有一个完全有效的选择寻找与ID元素first ,并且还具有类name 。)

This is a bug that is easy to overlook.

(这是一个容易忽略的错误。)

You might be happier in the long run choosing the id first-name (a hyphen rather than a period), instead.

(从长远来看,您可能会更快乐,而是选择id first-name (连字符而不是句点)。)

You can simplify your development tasks by strictly sticking to a naming convention.

(您可以通过严格遵守命名约定来简化开发任务。)

For example, if you limit yourself entirely to lower-case characters and always separate words with either hyphens or underscores (but not both, pick one and never use the other), then you have an easy-to-remember pattern.

(例如,如果您将自己完全限制为小写字母,并且始终使用连字符或下划线来分隔单词(但不能同时使用两个字符,请选择一个而不使用另一个),那么您将拥有一个易于记忆的模式。)

You will never wonder "was it firstName or FirstName ?"

(您将永远不会怀疑“是firstName还是FirstName ?”)

because you will always know that you should type first_name .

(因为您将永远知道应该键入first_name 。)

Prefer camel case?

(喜欢骆驼案吗?)

Then limit yourself to that, no hyphens or underscores, and always, consistently use either upper-case or lower-case for the first character, don't mix them.

(然后限制自己,不要使用连字符或下划线,并且始终对第一个字符使用大写或小写,请勿混用。)


A now very obscure problem was that at least one browser, Netscape 6, incorrectly treated id attribute values as case-sensitive .

(现在非常模糊的问题是,至少一个浏览器Netscape 6 错误地将id属性值视为区分大小写 。)

That meant that if you had typed id="firstName" in your HTML (lower-case 'f') and #FirstName { color: red } in your CSS (upper-case 'F'), that buggy browser would have failed to set the element's color to red.

(这意味着,如果您在HTML中输入id="firstName" (小写字母'f'),而在CSS中#FirstName { color: red } (大写字母'F'),则该多虫浏览器将无法将元素的颜色设置为红色。)

At the time of this edit, April 2015, I hope you aren't being asked to support Netscape 6. Consider this a historical footnote.

(在进行此编辑(2015年4月)时,希望您不会被要求支持Netscape6。请考虑一下这是一个历史脚注。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...