这是一个重复的问题,George 上一个问题的链接是我的答案
我遇到了一个问题,即选择器对动态生成的 javascript 内容不起作用。
最初的作品很好。一旦 for 循环生成更多 div,即使它具有相同的类,“鼠标悬停”css 样式也将不适用。
生成div的代码:
for (x; x < y; x++) {
output = output + '<div class="over">'+
'But not for these generated divs'+
'</div>';
}
$("#content").html(output);
Run Code Online (Sandbox Code Playgroud)
使用类“over”设置 div 样式的代码:
$(".over").hover(function () {
$(this).addClass("styling");
});
$(".over").mouseout(function () {
$(this).removeClass("styling");
});
Run Code Online (Sandbox Code Playgroud)