[英]Insert same html into element multiple times with jQuery
I want the user to click on a button and then have some html pop into an element. 我希望用户单击按钮,然后将一些html弹出到元素中。 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. 我认为使用empty()可以解决问题,但不能解决。 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>
The element is hidden the second time you click the button. 第二次单击该按钮时该元素将隐藏。 You can do: 你可以做:
$('#ul').show().html('...
$("#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: 当您执行.hide()时,将CSS选择器设置为“ display” =“ NONE”,您需要将其更改为如下所示:
$('#ul').html('<li>testing testing</li>').hide(1000, function() {
$(this).empty();
$(this).show();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.