这让我疯狂。我试图通过jQuery中的ID选择DOM元素并将其删除。尽管我尽了最大的努力,但jQuery总是给我一个未捕获的错误:语法错误,无法识别的表达式:#li-/media/photos/KObtu.jpggetElementByID工作,但jQuery的选择不是
但是,这个ID绝对存在于我的DOM中。所以我决定从图片中剪下jQuery并使用普通的'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>
非常感谢你提前。
你为何用如此怪异的ID? – Oriol
你试过逃避'/'吗? – nandu
@ user1556487:作为附注,ID和名称标记必须以字母([A-Za-z])开头,后面可以跟随任意数量的字母,数字([0-9]),连字符( “ - ”),下划线(“_”),冒号(“:”)和句点(“。”)。 type-id只要注意'.',因为jQuery将其视为一种特殊情况。 – Nope