这让我发疯。我正在尝试在 jQuery 中按 ID 选择一个 DOM 元素并将其删除。尽管我尽了最大的努力,jQuery 仍然给我 Uncaught Error: Syntax error, unrecognized expression: #li-/media/photos/KObtu.jpg
但是这个 id 确实存在于我的 DOM 中。所以我决定从图片中删除 jQuery 并使用普通的 ol' javascript。这很好用,但我不知道为什么。
function DeletePhoto() {
//Remove a photo from the server when the delete button is clicked
$("#personal-photo-list .delete").click( function() {
var photoId = $(this).attr("id").split("-")[1];
$.post("profile/DeletePhoto/", { "photo": photoId }, function(jsonObject){
$("#li-" + photoId).remove(); //What I actually want to call. Does not work
$("#li-/media/photos/KObtu.jpg").remove(); //I thought maybe using a variable was throwing things off. This does not work either
document.getElementById("li-/media/photos/KObtu.jpg").innerHTML="dsfsdfsdfsdf"; //The exact same id as the statements above. This command successfully replaces the text in the <li> tag
});
});
}
如果有人好奇,这里是 HTML 的片段:
<li id="li-/media/photos/KObtu.jpg">
<img src="/media/photos/KObtu.jpg">
<p> Uploaded on Sept. 23, 2012 | <a href="javascript:void(0)" id="a-/media/photos/KObtu.jpg" class="delete"> Delete </a> </p>
</li>
非常感谢你。