mirror of
https://github.com/Hopiu/jquery-mobile.git
synced 2026-03-17 14:30:28 +00:00
In starting markup, pages should now be identified with the attribute data-role="page". This allows us to then add ui-page programatically, hiding all non-active pages, and apply ui-page-active to one page at a time to show it. mobile.js is updated to find pages by this attribute now, instead of ui-page class. fixes issue 32
169 lines
No EOL
6.5 KiB
HTML
169 lines
No EOL
6.5 KiB
HTML
<!DOCTYPE html>
|
|
<html lang='en'><head><meta http-equiv='content-type' content='text/html; charset=UTF-8' /></head><body>
|
|
<div data-role="page">
|
|
<div class="ui-header">
|
|
<h1>.replaceWith()</h1>
|
|
|
|
</div>
|
|
<div class="ui-content ui-body ui-body-c" id="replaceWith1">
|
|
<h2 class="jq-clearfix roundTop section-title">
|
|
<span class="name">.replaceWith( newContent )</span> <span class="returns">Returns: <a class="return" href="http://docs.jquery.com/Types#jQuery">jQuery</a></span>
|
|
</h2>
|
|
<div class=" entry-details">
|
|
<p class="desc"><strong>Description: </strong>Replace each element in the set of matched elements with the provided new content.</p>
|
|
<ul class="signatures">
|
|
<li class="signature" id="replaceWith-newContent">
|
|
<h4 class="name">
|
|
<span class="versionAdded">version added: <a href="/category/version/1.2/">1.2</a></span>.replaceWith( newContent )</h4>
|
|
<p class="arguement"><strong>newContent</strong>The content to insert. May be an HTML string, DOM element, or jQuery object.</p>
|
|
</li>
|
|
<li class="signature" id="replaceWith-function">
|
|
<h4 class="name">
|
|
<span class="versionAdded">version added: <a href="/category/version/1.4/">1.4</a></span>.replaceWith( function )</h4>
|
|
<p class="arguement"><strong>function</strong>A function that returns an HTML string to replace the set of matched elements with.</p>
|
|
</li>
|
|
</ul>
|
|
<div class="longdesc">
|
|
<p>The <code>.replaceWith()</code> method allows us to remove content from the DOM and insert new content in its place with a single call. Consider this DOM structure:</p>
|
|
<pre><div class="container">
|
|
<div class="inner first">Hello</div>
|
|
<div class="inner second">And</div>
|
|
<div class="inner third">Goodbye</div>
|
|
</div></pre>
|
|
<p>We can replace the second inner <code><div></code> with specified HTML:</p>
|
|
<pre>$('.second').replaceWith('<h2>New heading</h2>');</pre>
|
|
<p>This results in the structure:</p>
|
|
<pre><div class="container">
|
|
<div class="inner first">Hello</div>
|
|
<h2>New heading</h2>
|
|
<div class="inner third">Goodbye</div>
|
|
</div></pre>
|
|
<p>We could equally target all inner <code><div></code> elements at once:</p>
|
|
<pre>$('.inner').replaceWith('<h2>New heading</h2>');</pre>
|
|
<p>This causes all of them to be replaced:</p>
|
|
<pre><div class="container">
|
|
<h2>New heading</h2>
|
|
<h2>New heading</h2>
|
|
<h2>New heading</h2>
|
|
</div></pre>
|
|
<p>Or, we could select an element to use as the replacement:</p>
|
|
<pre>$('.third').replaceWith($('.first'));</pre>
|
|
<p>This results in the DOM structure:</p>
|
|
<pre><div class="container">
|
|
<div class="inner second">And</div>
|
|
<div class="inner first">Hello</div>
|
|
</div></pre>
|
|
<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>
|
|
<p>The <code>.replaceWith()</code> method, like most jQuery methods, returns the jQuery object so that other methods can be chained onto it. However, it must be noted that the <emphasis role="italics">original</emphasis> jQuery object is returned. This object refers to the element that has been removed from the DOM, not the new element that has replaced it.</p>
|
|
<p>In jQuery 1.4 replaceWith, before, and after will also work on disconnected DOM nodes. For example if you were to do:</p>
|
|
<pre>$("<div></div>").replaceWith("<p></p>");</pre>
|
|
<p>Then you would end up with a jQuery set that contains only a paragraph.</p>
|
|
</div>
|
|
<h3>Examples:</h3>
|
|
<div id="entry-examples" class="entry-examples">
|
|
<div id="example-0">
|
|
<h4>Example: <span class="desc">On click, replace the button with a div containing the same word.</span>
|
|
</h4>
|
|
<pre><code class="example demo-code"><!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<style>
|
|
button { display:block; margin:3px; color:red; width:200px; }
|
|
div { color:red; border:2px solid blue; width:200px;
|
|
margin:3px; text-align:center; }
|
|
</style>
|
|
<script src="http://code.jquery.com/jquery-latest.js"></script>
|
|
</head>
|
|
<body>
|
|
<button>First</button>
|
|
<button>Second</button>
|
|
|
|
<button>Third</button>
|
|
<script>
|
|
|
|
$("button").click(function () {
|
|
$(this).replaceWith("<div>" + $(this).text() + "</div>");
|
|
});
|
|
</script>
|
|
</body>
|
|
</html></code></pre>
|
|
<h4>Demo:</h4>
|
|
<div class="demo code-demo"></div>
|
|
</div>
|
|
<div id="example-1">
|
|
<h4>Example: <span class="desc">Replace all the paragraphs with bold words.</span>
|
|
</h4>
|
|
<pre><code class="example demo-code"><!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<script src="http://code.jquery.com/jquery-latest.js"></script>
|
|
</head>
|
|
<body>
|
|
<p>Hello</p>
|
|
<p>cruel</p>
|
|
|
|
<p>World</p>
|
|
<script>$("p").replaceWith("<b>Paragraph. </b>");</script>
|
|
</body>
|
|
</html></code></pre>
|
|
<h4>Demo:</h4>
|
|
<div class="demo code-demo"></div>
|
|
</div>
|
|
<div id="example-2">
|
|
<h4>Example: <span class="desc">Replace all the paragraphs with empty div elements.</span>
|
|
</h4>
|
|
<pre><code class="example demo-code"><!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<style>
|
|
div { border:2px solid blue; margin:3px; }
|
|
</style>
|
|
<script src="http://code.jquery.com/jquery-latest.js"></script>
|
|
</head>
|
|
<body>
|
|
<p>Hello</p>
|
|
|
|
<p>cruel</p>
|
|
<p>World</p>
|
|
<script>$("p").replaceWith(document.createElement("div"));</script>
|
|
</body>
|
|
</html></code></pre>
|
|
<h4>Demo:</h4>
|
|
<div class="demo code-demo"></div>
|
|
</div>
|
|
<div id="example-3">
|
|
<h4>Example: <span class="desc">On click, replace each paragraph with a jQuery div object that is already in the DOM. Notice it doesn't clone the object but rather moves it to replace the paragraph.</span>
|
|
</h4>
|
|
<pre><code class="example demo-code"><!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<style>
|
|
div { border:2px solid blue; color:red; margin:3px; }
|
|
p { border:2px solid red; color:blue; margin:3px; cursor:pointer; }
|
|
</style>
|
|
<script src="http://code.jquery.com/jquery-latest.js"></script>
|
|
</head>
|
|
<body>
|
|
<p>Hello</p>
|
|
<p>cruel</p>
|
|
<p>World</p>
|
|
|
|
<div>Replaced!</div>
|
|
<script>
|
|
$("p").click(function () {
|
|
$(this).replaceWith($("div"));
|
|
});
|
|
|
|
</script>
|
|
</body>
|
|
</html></code></pre>
|
|
<h4>Demo:</h4>
|
|
<div class="demo code-demo"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</body></html> |