gpt4 book ai didi

JavaScript attr ('id' )和这个

转载 作者:行者123 更新时间:2023-12-02 19:16:51 25 4
gpt4 key购买 nike

所以我不太擅长 JavaScript,但我有点坚持以下几点:

所以,我得到了这个:

$(".post-click").click(function()
{
var classid = $(this).attr('id');
var postid = $(this).attr('id');
postid = postid.replace('post-click-id_', '');

alert("ID: " + classid + " PostID: " + postid);

$(this).replaceWith('<img SRC="assets/img/refresh.gif" ALT="" >');

$.post("likepost.php", { postid: postid } , function(data)
{
if(data.indexOf("Yes") >= 0)
{
//Question is about this part
}
else
{
//Question is about this part
}

});
});

现在,在"is"或其他部分:我怎样才能用 replaceWith 替换 $(this) 中的数据?我认为我可以使用 classid 来做到这一点,但我不太确定如何做到这一点。我想到了这一点:

$(classid).replaceWith('Yes, yes indeed.');
$(classid).replaceWith('Nope.');

我该如何完成这项工作?

最佳答案

假设我已经正确理解了这个问题,并且您正在尝试替换 $.post 回调中单击的元素,最简单的做法是在外部维护对该元素的引用的回调。这使您不必再次遍历 DOM 来重新选择已经选择过的元素:

var clicked = $(this);
$.post("likepost.php", { postid: postid } , function(data) {
if(data.indexOf("Yes") >= 0) {
clicked.replaceWith("Yes");
} else {
clicked.replaceWith("No");
}
});

您当前的尝试不起作用,因为 classid 只是一个表示 id 属性值的字符串。要从中创建 jQuery 对象,您需要将其附加到“#”以生成有效的选择器。

关于JavaScript attr ('id' )和这个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13027221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com