jQuery append while slideToggle
I am appending new data - $('#posts').append(da开发者_运维问答ta);
But I want this new data to .slideToggle() while appending.How can it be done?
You can do it if you have a single containing element.
$("<div>").append(data).hide().appendTo("#posts").slideDown();
Let me give you an example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript">
var content = '<p>four</p><p>five</p><p>six</p>';
$(function() {
$("#add").click(function() {
$("<div>").append(content).hide().appendTo("#container").slideDown(1000, function() {
$(this).contents().unwrap();
});
});
});
</script>
<style type="text/css">
div, p { padding: 0; margin: 0; border: 0 none; }
#container { background: yellow; border: 1px solid black; }
</style>
</head>
<body>
<div id="container">
<p>one</p>
<p>two</p>
<p>three</p>
</div>
<input id="add" type="button" value="Add Content">
</body>
</html>
Here the content is not one block, it's three (paragraphs). If it was one block you wouldn't need to wrap it like this.
精彩评论