我在删除多个具有相同 ID 的 href 链接时遇到问题。这是我的代码片段:
$('.delblk').click(function(e) {
e.preventDefault();
var id = $(this).attr('id').substr(7);
$.getJSON("../ajax_blocked.php?op=delete&id="+id, function(data) {
if (data) {
$("#delblk_"+id).each(function() {
$(this).remove();
});
}
});
我的 HTML 看起来像这样:
<a href="sent.php" id="delblk_7" class="delblk" ><span class="label label-important">Unblock</span></a>
<a href="sent.php" id="delblk_7" class="delblk" ><span class="label label-important">Unblock</span></a>
<a href="sent.php" id="delblk_8" class="delblk" ><span class="label label-important">Unblock</span></a>
它只删除第一个 href 链接,而不是两个。我错过了什么?
最佳答案
描述
问题是 id 在您的文档中必须是唯一的,请改用 class
属性。
所以你不能使用id (#)
选择器。
但是您可以使用Attribute Equals Selector
遍历您的元素,检查我的样本和这个jsFiddle Demonstration
w3c The id attribute specifies a unique id for an HTML element (the id attribute value must be unique within the HTML document).
示例
$("a[id='delblk_"+id+"']").each(function() {
// ...
});
更多信息
关于javascript - 如何使用 jquery 通过 ID 删除多个 href 链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9194457/