1 line
2.1 KiB
JSON
1 line
2.1 KiB
JSON
{"name":"replaceAll","type":"method","title":".replaceAll()","deprecated":null,"removed":null,"desc":"Replace each target element with the set of matched elements.","categories":["manipulation/dom-replacement","version/1.2"],"entries":[{"return":"jQuery","signatures":{"added":"1.2","argument":{"desc":"A selector string, jQuery object, DOM element, or array of elements indicating which element(s) to replace.","type":[{"name":"Selector"},{"name":"jQuery"},{"name":"Array"},{"name":"Element"}],"name":"target"}},"examples":{"desc":"Replace all the paragraphs with bold words.","code":"\n$( \"<b>Paragraph. </b>\" ).replaceAll( \"p\" );\n","html":"\n<p>Hello</p>\n<p>cruel</p>\n<p>World</p>\n"},"longdesc":"\n <p>The <code>.replaceAll()</code> method is corollary to <code><a href=\"/replaceWith/\">.replaceWith()</a></code>, but with the source and target reversed. Consider this DOM structure:</p>\n <pre><code>\n<div class=\"container\">\n <div class=\"inner first\">Hello</div>\n <div class=\"inner second\">And</div>\n <div class=\"inner third\">Goodbye</div>\n</div>\n </code></pre>\n <p>We can create an element, then replace other elements with it:</p>\n <pre><code>$( \"<h2>New heading</h2>\" ).replaceAll( \".inner\" );</code></pre>\n <p>This causes all of them to be replaced:</p>\n <pre><code>\n<div class=\"container\">\n <h2>New heading</h2>\n <h2>New heading</h2>\n <h2>New heading</h2>\n</div>\n </code></pre>\n <p>Or, we could select an element to use as the replacement:</p>\n <pre><code>\n$( \".first\" ).replaceAll( \".third\" );\n </code></pre>\n <p>This results in the DOM structure:</p>\n <pre><code>\n<div class=\"container\">\n <div class=\"inner second\">And</div>\n <div class=\"inner first\">Hello</div>\n</div>\n </code></pre>\n <p>From this example, we can see that the selected element replaces the target by being moved from its old location, not by being cloned.</p>\n "}]} |