【发布时间】:2023-03-24 16:07:01
【问题描述】:
我正在尝试删除动态创建的 DOM 元素,但由于某种原因我无法让它工作。
它可以使用我指定的 ID 正常创建对象,但不会删除。
对if 语句的检查正在运行,因为它会打印出console.log()。
有什么想法吗?
function displayLive()
{
var previous = null;
var current = null;
setInterval(function()
{
$.ajax({
url: '/showLive',
dataType: 'json',
contentType: 'application/json',
success: function(response)
{
current = JSON.stringify(response);
if(previous !== current)
{
var obj = JSON.parse(response);
console.log(obj);
for(var i = 0; i < obj.active.length; i++)
{
if(obj.active[i].active === true)
{
$('.left').prepend($('<div/>', {class: 'profTemp', id: obj.active[i].userNameData}).append(
$('<img/>', {src: obj.active[i].profiler, width: 40, height: 40}),
$('<span/>', {text: " " + obj.active[i].userNameData})));
}
else if(obj.active[i].active === false)
{
$('#%s' , obj.active[i].userNameData).remove();
console.log("getting in false");
}
}
}
}
});
previous = current;
}, 2000);
}
【问题讨论】:
-
我认为
%s仅在console方法中存在。我不知道jQuery是否支持它。尝试连接 id。 -
我也试过了,还是不行
-
也可能是 userNameData 有一个 '.' (点)在那里导致jquery出现问题并找到该项目。 $(document.getElementById(obj.active[i].userNameData)).remove() 可能会有所帮助
-
成功了!谢谢大家!
-
别担心,如果你能检查一下,我已经给出了答案
标签: javascript jquery html json dom