我试图在成功函数上删除此元素,但它没有这样做!!
jQuery 文件:
$(function () {
//More Button
$('.more').live("click", function () {
var ID = $(this).attr("id");
if (ID) {
$("#more" + ID).html('<img src="moreajax.gif" />');
$.ajax({
type: "POST",
url: "ajax_more.php",
data: "lastmsg=" + ID,
cache: false,
success: function (html) {
$("ul.statuses").append(html);
// my problem is here
$("#more" + ID).remove();
}
});
}
else {
$(".morebox").html('The End');
}
return false;
});
});
这是 html 文件: 单击此按钮可检索更多结果!
echo'<div id="more'. $dateTime.'" class="morebox">
<a href="#" class="more" id="'.$dateTime.'">more</a>';
最佳答案
如果您的 id
中有空格,那就是您的问题。空格在 id
属性中有效,但它会扰乱您在此处使用的 CSS 选择器(因为它未正确转义):
$("#more" + ID).remove();
例如,使用您在另一个答案的评论中引用的 ID,最终将是:
$("#more2010-09-20 12:53:43").remove();
...这是一个无效的选择器(我很确定;至少,它不是您正在寻找的选择器)。
最简单的事情可能是确保您的 $dateTime
变量的值中不包含任何空格。为了更好地衡量,我也会避免使用冒号(:
),但我可能只是偏执(不,我不是,见下文)。
例如,您可以更改输出这些内容的 PHP 代码以使用 str_replace
看起来像这样(我不是 PHP 专家,如果有轻微的语法问题,我深表歉意,但你明白了要点):
$baseId = str_replace(':', '', str_replace(' ', '_', $dateTime));
echo'<div id="more'. $baseId.'" class="morebox"><a href="#" class="more" id="'.$baseId.'">more</a>';
<小时/>
有关 CSS 选择器的详细信息:有许多有效的 id
值无法在 CSS 选择器中使用,除非您转义某些字符。 id
中几乎任何内容都是有效的,但是 ID you use with a CSS selector必须使用其中的子集或正确转义:
In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A1 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, or a hyphen followed by a digit. Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (see next item). For instance, the identifier "B&W?" may be written as "B\&W\?" or "B\26 W\3F".
关于php - 我在使用 jQuery 删除元素时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3754270/