1 line
2.4 KiB
JSON
1 line
2.4 KiB
JSON
{"name":"id","type":"selector","title":"ID Selector (\"#id\")","deprecated":null,"removed":null,"desc":"Selects a single element with the given id attribute. ","categories":["selectors/basic-css-selectors","version/1.0"],"entries":[{"return":"","signatures":{"added":"1.0","argument":{"desc":"An ID to search for, specified via the id attribute of an element.","name":"id","type":"String"}},"examples":[{"desc":"Select the element with the id \"myDiv\" and give it a red border.","code":"\n$( \"#myDiv\" ).css( \"border\", \"3px solid red\" );\n","html":"\n<div id=\"notMe\"><p>id=\"notMe\"</p></div>\n<div id=\"myDiv\">id=\"myDiv\"</div>\n","css":"\n div {\n width: 90px;\n height: 90px;\n float: left;\n padding: 5px;\n margin: 5px;\n background-color: #eee;\n }\n"},{"desc":"Select the element with the id \"myID.entry[1]\" and give it a red border. Note how certain characters must be escaped with backslashes.","code":"\n$( \"#myID\\\\.entry\\\\[1\\\\]\" ).css( \"border\", \"3px solid red\" );\n","html":"\n<div id=\"myID.entry[0]\">id=\"myID.entry[0]\"</div>\n<div id=\"myID.entry[1]\">id=\"myID.entry[1]\"</div>\n<div id=\"myID.entry[2]\">id=\"myID.entry[2]\"</div>\n","css":"\n div {\n width: 300px;\n float: left;\n padding: 2px;\n margin: 3px;\n background-color: #eee;\n }\n"}],"longdesc":"\n <p>For id selectors, jQuery uses the JavaScript function <code>document.getElementById()</code>, which is extremely efficient. When another selector is attached to the id selector, such as <code>h2#pageTitle</code>, jQuery performs an additional check before identifying the element as a match.</p>\n <p>Calling <code>jQuery()</code> (or <code>$()</code>) with an id selector as its argument will return a jQuery object containing a collection of either zero or one DOM element.</p>\n <p>Each <code>id</code> 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.</p>\n <p>If the id contains characters like periods or colons you have to <a href=\"http://learn.jquery.com/faq/how-do-i-select-an-element-by-an-id-that-has-characters-used-in-css-notation/\">escape those characters with backslashes</a>.</p>\n "}],"slug":"id-selector"} |