is this Valid HTML for jquery
I have the following开发者_运维技巧 HTML.
div#main-content div#right-col div#post-show div#post-img div#post-img-large{
}
div#main-content div#right-col div#post-show div#post-img div#post-img-large
img#post-img-large{
}
As you can see, I am using same id for div and img.
Is this allowed? I know it works fine, but will jQuery get confused if i refer to the id post-img-large?
IDs should not be used on more than one element on any given page. They are used to uniquely identify elements.
Now, in your particular example, if you are using jQuery, there are always ways of selecting the elements you want by qualifying your selectors with more information (div#ID, img#ID) - jQuery will not be confused, but you should stick to the standards and not reuse IDs.
From the W3c: http://www.w3.org/TR/html4/struct/global.html (html4, but it still applies)
7.5.2 Element identifiers: the id and class attributes
Attribute definitions
id = name [CS]
This attribute assigns a name to an element. This name must be unique in a document.
class = cdata-list [CS]
This attribute assigns a class name or set of class names to an element. Any number of elements may be assigned the same class name or names. Multiple class names must be separated by white space characters.
From a previous experience:
For id selectors, jQuery uses the JavaScript function document.getElementById(), which is extremely efficient. When another selector is attached to the id selector, such as h2#pageTitle, jQuery performs an additional check before identifying the element as a match.
Each id value must be used only once within a document. If more than one element has been assigned the same ID, queries that use that ID will only select the first matched element in the DOM. This behavior should not be relied on, however; a document with more than one element using the same ID is invalid.
Source: http://api.jquery.com/id-selector/
Check every code in http://www.w3.org
That's not a valid html, id
s must be unique.
And yes, it's possible that jquery won't work as expected because of the duplicated ids. In fact I saw a question with broken functionality here on SO just a couple of days ago.
精彩评论