This question already has an answer here:
这个问题已经有了答案:
- Event binding on dynamically created elements? 21 answers
- 动态创建的元素上的事件绑定?21日答案
I'm generating html tags with jQuery. Like this
我用jQuery生成html标签。像这样
jQuery:
jQuery:
$(document).ready(function() {
$('#example').DataTable();
var cas = ["1", "2", "3", "4", "5", "6"];
var text = "";
var i;
for (i = 1; i <= cas.length; i++) {
text += '<li class="noactive" ></li>';
}
jQuery("#bitacorapagination").html(text);
jQuery('.active').click(function () {
suplementos(this);
jQuery(".active").removeClass("active").addClass("noactive");
jQuery(this).removeClass("active").addClass("noactive");
console.log("Event");
});
jQuery('.noactive').click(function () {
suplementos(this);
jQuery(".active").removeClass("active").addClass("noactive");
jQuery(this).removeClass("noactive").addClass("active");
console.log("Event");
});
});
html code:
html代码:
<ul>
<li id="bitacorapagination"></li>
<ul>
what I need is to remove and add some class to the <ul>
element generated by jQuery , but the event doesn't activate on the event onclick()
我需要删除并添加一些类到jQuery生成的
-
元素中,但是事件在事件onclick()上没有激活
Do you have any idea how to solve this?
你知道怎么解决这个问题吗?
1 个解决方案
#1
1
you are looping and creating mulitple li's with the same id, switch to a class
您正在循环并创建具有相同id的mulitple li,切换到类。
'<li class="noactive" ></li>';
#1
1
you are looping and creating mulitple li's with the same id, switch to a class
您正在循环并创建具有相同id的mulitple li,切换到类。
'<li class="noactive" ></li>';