【发布时间】:2017-04-22 12:14:47
【问题描述】:
这里是my example。
通过单击添加按钮,我添加了一张用户卡。 “清除按钮”删除所有卡片。如何通过点击每张卡片中的“关闭”图标一一移除卡片?
HTML 文件
<div class="header">
<button id="clear" class="button clear">Clear</button>
<button id="button" class="button add">Add user</button>
</div>
<div id="content">
</div>
JS 文件
var root = 'https://jsonplaceholder.typicode.com';
var index = 0;
$.ajax({
url: root + '/posts/1/comments',
method: 'GET'
}).then(function(data) {
$("#button").click(function() {
var notificationMessage = "Oops, there are no more user cards to display";
if (index >= data.length ) {
return alert(notificationMessage);
}
$("#content").append('<div id="card"><div class="title"><div class="image"></div><div id="name">'
+ data[index].name + '</div><span id="close"></span></div><div id="description">'
+ data[index].body + '<a href="mailto:" id="email">'
+ data[index].email + '</a></div></div>'
);
index++;
// remove all cards from a list and return index equally [0], to be able add user card again.
$("#clear").click(function() {
$("#card").remove();
index = 0;
});
});
});
//How to remove card by clicking on the close button?
【问题讨论】:
-
不要重复使用id。尝试使用类
-
非常感谢大家!我是 jquery 的初学者。一个月前才开始玩。
标签: javascript jquery ajax