简体   繁体   English

jQuery使用html()设置内容后,如何使new元素事件和原始元素绑定同一事件?

[英]jquery use html () after setting the content, how to make the new elements event and original elements bind the same event?

HTML: HTML:

<div id="1">
<div>
<ul>
<li><button id="button">a</button></li>
<li><span>1</span></li>
</ul>
</div>
</div>
<div id="2">
<ul>
<li></li>
</ul>
</div>

JQUERY: JQUERY:

$("#button").live("click",function(){
var s = Number($(this).parents("ul").find("span").text());
$(this).parents("ul").find("span").text(s+1);
$("#2 li").html($("#1 li").html());
});

The problem is that when I click on the button , span's number add number 1 and DIV#2 set up a new button , how can i add DIV#1 span number when i click DIV#2 new , I means how this function can find the right parents and span? 问题是,当我单击按钮时,跨度的数字加1并DIV#2设置了一个新按钮,当我单击DIV#2新建时,如何添加DIV#1跨度号,我的意思是该函数可以找到合适的父母和跨度?

here is DEMO 这是演示

i want to add number 1 when i click new button ,that's all,thanks! 我想在单击新按钮时添加数字1,就这样,谢谢!

You can check changed Demo . 您可以检查已更改的Demo

$("#button").live("click",function(){        
    var span = $('#1 span');
    span.html(Number(span.html())+1);    
    $("#2 li").html($("#1 li").html());
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM