|
26 | 26 | </signature> |
27 | 27 | <desc>Insert content, specified by the parameter, before each element in the set of matched elements.</desc> |
28 | 28 | <longdesc> |
29 | | - <p>The <code>.before()</code> and <code><a href="/insertBefore">.insertBefore()</a></code> methods perform the same task. The major difference is in the syntax-specifically, in the placement of the content and target. With<code> .before()</code>, the selector expression preceding the method is the container before which the content is inserted. With <code>.insertBefore()</code>, on the other hand, the content precedes the method, either as a selector expression or as markup created on the fly, and it is inserted before the target container.</p> |
| 29 | + <p>The <code>.before()</code> and <code><a href="/insertBefore">.insertBefore()</a></code> methods perform the same task. The major difference is in the syntax-specifically, in the placement of the content and target. With <code>.before()</code>, the selector expression preceding the method is the container before which the content is inserted. With <code>.insertBefore()</code>, on the other hand, the content precedes the method, either as a selector expression or as markup created on the fly, and it is inserted before the target container.</p> |
30 | 30 | <p>Consider the following HTML:</p> |
31 | | - <pre><code><div class="container"> |
| 31 | + <pre><code> |
| 32 | +<div class="container"> |
32 | 33 | <h2>Greetings</h2> |
33 | 34 | <div class="inner">Hello</div> |
34 | 35 | <div class="inner">Goodbye</div> |
35 | | -</div></code></pre> |
| 36 | +</div> |
| 37 | + </code></pre> |
36 | 38 | <p>You can create content and insert it before several elements at once:</p> |
37 | | - <pre><code>$('.inner').before('<p>Test</p>');</code></pre> |
| 39 | + <pre><code> |
| 40 | +$( ".inner" ).before( "<p>Test</p>" ); |
| 41 | + </code></pre> |
38 | 42 | <p>Each inner <code><div></code> element gets this new content:</p> |
39 | | - <pre><code><div class="container"> |
| 43 | + <pre><code> |
| 44 | +<div class="container"> |
40 | 45 | <h2>Greetings</h2> |
41 | 46 | <p>Test</p> |
42 | 47 | <div class="inner">Hello</div> |
43 | 48 | <p>Test</p> |
44 | 49 | <div class="inner">Goodbye</div> |
45 | | -</div></code></pre> |
| 50 | +</div> |
| 51 | + </code></pre> |
46 | 52 | <p>You can also select an element on the page and insert it before another:</p> |
47 | | - <pre><code>$('.container').before($('h2'));</code></pre> |
| 53 | + <pre><code> |
| 54 | +$( ".container" ).before( $( "h2" ) ); |
| 55 | + </code></pre> |
48 | 56 | <p>If an element selected this way is inserted into a single location elsewhere in the DOM, it will be moved before the target (not cloned):</p> |
49 | | - <pre><code><h2>Greetings</h2> |
| 57 | + <pre><code> |
| 58 | +<h2>Greetings</h2> |
50 | 59 | <div class="container"> |
51 | 60 | <div class="inner">Hello</div> |
52 | 61 | <div class="inner">Goodbye</div> |
53 | | -</div></code></pre> |
| 62 | +</div> |
| 63 | + </code></pre> |
54 | 64 | <p>If there is more than one target element, however, cloned copies of the inserted element will be created for each target after the first.</p> |
55 | 65 | <p>In jQuery 1.4, <code>.before()</code> and <code>.after()</code> will also work on disconnected DOM nodes:</p> |
56 | | - <pre><code>$("<div/>").before("<p></p>");</code></pre> |
| 66 | + <pre><code> |
| 67 | +$( "<div/>" ).before( "<p></p>" ); |
| 68 | + </code></pre> |
57 | 69 | <p>The result is a jQuery set that contains a paragraph and a div (in that order).</p> |
58 | 70 | <h4 id="additional-arguments">Additional Arguments</h4> |
59 | 71 | <p>Similar to other content-adding methods such as <code><a href="http://api.jquery.com/prepend/">.prepend()</a></code> and <code><a href="http://api.jquery.com/after/">.after()</a></code>, <code>.before()</code> also supports passing in multiple arguments as input. Supported input includes DOM elements, jQuery objects, HTML strings, and arrays of DOM elements.</p> |
60 | 72 | <p>For example, the following will insert two new <code><div></code>s and an existing <code><div></code> before the first paragraph:</p> |
61 | | - <pre><code>var $newdiv1 = $('<div id="object1"/>'), |
62 | | - newdiv2 = document.createElement('div'), |
63 | | - existingdiv1 = document.getElementById('foo'); |
| 73 | + <pre><code> |
| 74 | +var $newdiv1 = $( "<div id='object1'/>" ), |
| 75 | + newdiv2 = document.createElement( "div" ), |
| 76 | + existingdiv1 = document.getElementById( "foo" ); |
64 | 77 |
|
65 | | -$('p').first().before($newdiv1, [newdiv2, existingdiv1]); |
| 78 | +$( "p" ).first().before( $newdiv1, [ newdiv2, existingdiv1 ] ); |
66 | 79 | </code></pre> |
67 | | - <p>Since <code>.before()</code> can accept any number of additional arguments, the same result can be achieved by passing in the three <code><div></code>s as three separate arguments, like so: <code>$('p').first().before($newdiv1, newdiv2, existingdiv1)</code>. The type and number of arguments will largely depend on how you collect the elements in your code.</p> |
| 80 | + <p>Since <code>.before()</code> can accept any number of additional arguments, the same result can be achieved by passing in the three <code><div></code>s as three separate arguments, like so: <code>$( "p" ).first().before( $newdiv1, newdiv2, existingdiv1 )</code>. The type and number of arguments will largely depend on how you collect the elements in your code.</p> |
68 | 81 | </longdesc> |
69 | 82 | <example> |
70 | 83 | <desc>Inserts some HTML before all paragraphs.</desc> |
71 | | - <code><![CDATA[$("p").before("<b>Hello</b>");]]></code> |
72 | | - <css><![CDATA[p { background:yellow; }]]></css> |
73 | | - <html><![CDATA[<p> is what I said...</p>]]></html> |
| 84 | + <code><![CDATA[ |
| 85 | +$( "p" ).before( "<b>Hello</b>" ); |
| 86 | +]]></code> |
| 87 | + <css><![CDATA[ |
| 88 | + p { |
| 89 | + background: yellow; |
| 90 | + } |
| 91 | + ]]></css> |
| 92 | + <html><![CDATA[ |
| 93 | +<p> is what I said...</p> |
| 94 | +]]></html> |
74 | 95 | </example> |
75 | 96 | <example> |
76 | 97 | <desc>Inserts a DOM element before all paragraphs.</desc> |
77 | | - <code><![CDATA[$("p").before( document.createTextNode("Hello") );]]></code> |
78 | | - <css><![CDATA[p { background:yellow; }]]></css> |
79 | | - <html><![CDATA[<p> is what I said...</p>]]></html> |
| 98 | + <code><![CDATA[ |
| 99 | +$( "p" ).before( document.createTextNode( "Hello" ) ); |
| 100 | +]]></code> |
| 101 | + <css><![CDATA[ |
| 102 | + p { |
| 103 | + background: yellow; |
| 104 | + } |
| 105 | + ]]></css> |
| 106 | + <html><![CDATA[ |
| 107 | +<p> is what I said...</p> |
| 108 | +]]></html> |
80 | 109 | </example> |
81 | 110 | <example> |
82 | 111 | <desc>Inserts a jQuery object (similar to an Array of DOM Elements) before all paragraphs.</desc> |
83 | | - <code><![CDATA[$("p").before( $("b") );]]></code> |
84 | | - <css><![CDATA[p { background:yellow; }]]></css> |
85 | | - <html><![CDATA[<p> is what I said...</p><b>Hello</b>]]></html> |
| 112 | + <code><![CDATA[ |
| 113 | +$( "p" ).before( $( "b" ) ); |
| 114 | +]]></code> |
| 115 | + <css><![CDATA[ |
| 116 | + p { |
| 117 | + background: yellow; |
| 118 | + } |
| 119 | + ]]></css> |
| 120 | + <html><![CDATA[ |
| 121 | +<p> is what I said...</p><b>Hello</b> |
| 122 | +]]></html> |
86 | 123 | </example> |
87 | 124 | <category slug="manipulation/dom-insertion-outside"/> |
88 | 125 | <category slug="version/1.0"/> |
|
0 commit comments