mirror of
https://github.com/Hopiu/jquery-mobile.git
synced 2026-03-17 14:30:28 +00:00
165 lines
No EOL
6.6 KiB
HTML
165 lines
No EOL
6.6 KiB
HTML
<!DOCTYPE html>
|
|
<html lang='en'><head>
|
|
<meta charset="utf-8" /><meta http-equiv='content-type' content='text/html; charset=UTF-8' /></head><body>
|
|
<div data-role="page">
|
|
<div data-role="header">
|
|
<h1>.html()</h1>
|
|
|
|
</div>
|
|
|
|
<div data-role="content" data-theme="c" id="html1">
|
|
<h2 class="jq-clearfix roundTop section-title">
|
|
<span class="name">.html()</span> <span class="returns">Returns: <a class="return" href="http://docs.jquery.com/Types#String">String</a></span>
|
|
</h2>
|
|
<div class=" entry-details">
|
|
<p class="desc"><strong>Description: </strong>Get the HTML contents of the first element in the set of matched elements.</p>
|
|
<ul class="signatures"><li class="signature" id="html"><h4 class="name">
|
|
<span class="versionAdded">version added: <a href="/category/version/1.0/">1.0</a></span>.html()</h4></li></ul>
|
|
<div class="longdesc">
|
|
<p>This method is not available on XML documents.</p>
|
|
<p>In an HTML document, we can use <code>.html()</code> to get the contents of any element. If the selector expression matches more than one element, only the first one's HTML content is returned. Consider this code:</p>
|
|
<pre>$('div.demo-container').html();</pre>
|
|
<p>In order for the following <code><div></code>'s content to be retrieved, it would have to be the first one with <code>class="demo-container"</code> in the document:</p>
|
|
<pre><div class="demo-container">
|
|
<div class="demo-box">Demonstration Box</div>
|
|
</div></pre>
|
|
<p>The result would look like this:</p>
|
|
<pre><div class="demo-box">Demonstration Box</div></pre>
|
|
</div>
|
|
<h3>Example:</h3>
|
|
<div id="entry-examples" class="entry-examples"><div id="example-0">
|
|
<h4><span class="desc">Click a paragraph to convert it from html to text.</span></h4>
|
|
<pre><code class="example demo-code"><!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<style>
|
|
p { margin:8px; font-size:20px; color:blue;
|
|
cursor:pointer; }
|
|
b { text-decoration:underline; }
|
|
button { cursor:pointer; }
|
|
</style>
|
|
<script src="http://code.jquery.com/jquery-latest.js"></script>
|
|
</head>
|
|
<body>
|
|
<p>
|
|
|
|
<b>Click</b> to change the <span id="tag">html</span>
|
|
</p>
|
|
<p>
|
|
|
|
to a <span id="text">text</span> node.
|
|
</p>
|
|
<p>
|
|
This <button name="nada">button</button> does nothing.
|
|
</p>
|
|
<script>
|
|
$("p").click(function () {
|
|
var htmlStr = $(this).html();
|
|
$(this).text(htmlStr);
|
|
});
|
|
</script>
|
|
</body>
|
|
</html></code></pre>
|
|
<h4>Demo:</h4>
|
|
<div class="demo code-demo"></div>
|
|
</div></div>
|
|
</div>
|
|
</div>
|
|
<div data-role="content" data-theme="c" id="html2">
|
|
<h2 class="jq-clearfix roundTop section-title">
|
|
<span class="name">.html( htmlString )</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>Set the HTML contents of each element in the set of matched elements.</p>
|
|
<ul class="signatures">
|
|
<li class="signature" id="html-htmlString">
|
|
<h4 class="name">
|
|
<span class="versionAdded">version added: <a href="/category/version/1.0/">1.0</a></span>.html( htmlString )</h4>
|
|
<p class="arguement"><strong>htmlString</strong>A string of HTML to set as the content of each matched element.</p>
|
|
</li>
|
|
<li class="signature" id="html-functionindex, html">
|
|
<h4 class="name">
|
|
<span class="versionAdded">version added: <a href="/category/version/1.4/">1.4</a></span>.html( function(index, html) )</h4>
|
|
<p class="arguement"><strong>function(index, html)</strong>A function returning the HTML content to set. Receives the index position of the element in the set and the old HTML value as arguments.</p>
|
|
</li>
|
|
</ul>
|
|
<div class="longdesc">
|
|
<p>The <code>.html()</code> method is not available in XML documents. </p>
|
|
<p>When we use <code>.html()</code> to set elements' contents, any contents that were in those elements is completely replaced by the new contents. Consider the following HTML:</p>
|
|
<pre><div class="demo-container">
|
|
<div class="demo-box">Demonstration Box</div>
|
|
</div></pre>
|
|
<p>We can set the HTML contents of <code><div class="demo-container"></code> like so:</p>
|
|
<pre>$('div.demo-container')
|
|
.html('<p>All new content. <em>You bet!</em></p>');</pre>
|
|
<p>That line of code will replace everything inside <code><div class="demo-container"></code>:</p>
|
|
<pre><div class="demo-container">
|
|
<p>All new content. <em>You bet!</em></p>
|
|
</div></pre>
|
|
<p>As of jQuery 1.4, the <code>.html()</code> method allows us to set the HTML content by passing in a function.</p>
|
|
<pre>$('div.demo-container').html(function() {
|
|
var emph = '<em>' + $('p').length + ' paragraphs!</em>';
|
|
return '<p>All new content for ' + emph + '</p>';
|
|
});</pre>
|
|
<p>Given a document with six paragraphs, this example will set the HTML of <code><div class="demo-container"></code> to <code><p>All new content for <em>6 paragraphs!</em></p></code>.</p>
|
|
</div>
|
|
<h3>Examples:</h3>
|
|
<div id="entry-examples" class="entry-examples">
|
|
<div id="example-0">
|
|
<h4>Example: <span class="desc">Add some html to each div.</span>
|
|
</h4>
|
|
<pre><code class="example demo-code"><!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<style>
|
|
|
|
.red { color:red; }
|
|
</style>
|
|
<script src="http://code.jquery.com/jquery-latest.js"></script>
|
|
</head>
|
|
<body>
|
|
<span>Hello</span>
|
|
<div></div>
|
|
<div></div>
|
|
<div></div>
|
|
<script>$("div").html("<span class='red'>Hello <b>Again</b></span>");</script>
|
|
</body>
|
|
</html></code></pre>
|
|
<h4>Demo:</h4>
|
|
<div class="demo code-demo"></div>
|
|
</div>
|
|
<div id="example-1">
|
|
<h4>Example: <span class="desc">Add some html to each div then immediately do further manipulations to the inserted html.</span>
|
|
</h4>
|
|
<pre><code class="example demo-code"><!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<style>
|
|
div { color:blue; font-size:18px; }
|
|
</style>
|
|
<script src="http://code.jquery.com/jquery-latest.js"></script>
|
|
</head>
|
|
<body>
|
|
<div></div>
|
|
<div></div>
|
|
<div></div>
|
|
<script>
|
|
|
|
$("div").html("<b>Wow!</b> Such excitement...");
|
|
$("div b").append(document.createTextNode("!!!"))
|
|
.css("color", "red");
|
|
|
|
</script>
|
|
</body>
|
|
</html></code></pre>
|
|
<h4>Demo:</h4>
|
|
<div class="demo code-demo"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
</body></html> |