admin管理员组文章数量:1023099
I want the user to click on a button and then have some html pop into an element. I did so and it works but it only works once. If they click the button again, nothing happens. I thought using empty() would fix the problem but it doesn't. What's wrong with my code.
<script type="text/javascript" >
$(document).ready( function() {
$('#button').on('click', function () {
$('#ul').html('<li>testing testing</li>').hide(1000, function() {
$(this).empty();
});
});
});
</script>
<input type="button" value="click me" id="button" />
<ul id="ul">
</ul>
I want the user to click on a button and then have some html pop into an element. I did so and it works but it only works once. If they click the button again, nothing happens. I thought using empty() would fix the problem but it doesn't. What's wrong with my code.
<script type="text/javascript" >
$(document).ready( function() {
$('#button').on('click', function () {
$('#ul').html('<li>testing testing</li>').hide(1000, function() {
$(this).empty();
});
});
});
</script>
<input type="button" value="click me" id="button" />
<ul id="ul">
</ul>
Share
Improve this question
asked Oct 5, 2012 at 18:16
thank_youthank_you
11.1k19 gold badges106 silver badges194 bronze badges
5 Answers
Reset to default 3The element is hidden the second time you click the button. You can do:
$('#ul').show().html('...
http://jsfiddle/KXkgZ/
as you want to pop html each time you click on button you should use append
$('#ul').append('<li>testing testing</li>');
see append
$("#button").click(function(){
$("#ul").append("testing testing");
});
jQuery('<li>testing testing</li>').appendTo('#ul').hide(1000, function() {
$(this).remove();
});
When you do a .hide() you set the CSS selector "display" = "NONE" You need to change it to something like this:
$('#ul').html('<li>testing testing</li>').hide(1000, function() {
$(this).empty();
$(this).show();
});
I want the user to click on a button and then have some html pop into an element. I did so and it works but it only works once. If they click the button again, nothing happens. I thought using empty() would fix the problem but it doesn't. What's wrong with my code.
<script type="text/javascript" >
$(document).ready( function() {
$('#button').on('click', function () {
$('#ul').html('<li>testing testing</li>').hide(1000, function() {
$(this).empty();
});
});
});
</script>
<input type="button" value="click me" id="button" />
<ul id="ul">
</ul>
I want the user to click on a button and then have some html pop into an element. I did so and it works but it only works once. If they click the button again, nothing happens. I thought using empty() would fix the problem but it doesn't. What's wrong with my code.
<script type="text/javascript" >
$(document).ready( function() {
$('#button').on('click', function () {
$('#ul').html('<li>testing testing</li>').hide(1000, function() {
$(this).empty();
});
});
});
</script>
<input type="button" value="click me" id="button" />
<ul id="ul">
</ul>
Share
Improve this question
asked Oct 5, 2012 at 18:16
thank_youthank_you
11.1k19 gold badges106 silver badges194 bronze badges
5 Answers
Reset to default 3The element is hidden the second time you click the button. You can do:
$('#ul').show().html('...
http://jsfiddle/KXkgZ/
as you want to pop html each time you click on button you should use append
$('#ul').append('<li>testing testing</li>');
see append
$("#button").click(function(){
$("#ul").append("testing testing");
});
jQuery('<li>testing testing</li>').appendTo('#ul').hide(1000, function() {
$(this).remove();
});
When you do a .hide() you set the CSS selector "display" = "NONE" You need to change it to something like this:
$('#ul').html('<li>testing testing</li>').hide(1000, function() {
$(this).empty();
$(this).show();
});
本文标签: javascriptInsert same html into element multiple times with jQueryStack Overflow
版权声明:本文标题:javascript - Insert same html into element multiple times with jQuery - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745529892a2154699.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论