我将对象数组中的值附加到 div 标签。追加工作正常,但追加的 HTML 元素上的点击事件不起作用
let lists = [{name : "one", desc : "test"}, {name : "two", desc : "desc"}]
for(let a in lists)
$('#list').append('<div class="listing"><li id="' + lists[a].name +'" data-list-name="'+ lists[a].name +'"><h3>'+lists[a].name +'</h3></li><span class="view-more"></span><div class="view-more-container"><p class="list-text"><b>'+lists[a].desc +'</b></p></div></div>');
此处按预期附加了值,但附加后类视图上的单击功能-more 不起作用。它适用于直接值,不适用于附加值
答案 0 :(得分:0)
变量声明中的小错误,缺少名称
let lists = [{name : "one", desc : "test"}, {name : "two", desc : "desc"}]
for(let a in lists)
$('#list').append('<div class="listing"><li id="' + lists[a].name +'" data-list-name="'+ lists[a].name +'"><h3>'+lists[a].name +'</h3></li><span class="view-more"></span><div class="view-more-container"><p class="list-text"><b>'+lists[a].desc +'</b></p></div></div>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<div id="list">
</div>