Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AJAX中的动态附件成功_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript AJAX中的动态附件成功

Javascript AJAX中的动态附件成功,javascript,jquery,ajax,Javascript,Jquery,Ajax,我是网络开发新手 我已经从ajax成功函数中呈现了文本。我想在每个文本行下添加画布 在AJAX成功的内部,我尝试了以下两种方法,文本成功呈现,文本id成功动态分配,但它们都失败了,无法读取null的属性'appendChild',我认为document.getElementById('result'+idx').appendChild(canv)有问题就像我使用document.body.appendChild(canv)我得到画布,但不是在所需的位置 我不知道为什么getElementById

我是网络开发新手

我已经从ajax成功函数中呈现了文本。我想在每个文本行下添加画布

在AJAX成功的内部,我尝试了以下两种方法,文本成功呈现,文本id成功动态分配,但它们都失败了,
无法读取null的属性'appendChild',我认为
document.getElementById('result'+idx').appendChild(canv)有问题就像我使用
document.body.appendChild(canv)我得到画布,但不是在所需的位置

我不知道为什么
getElementById
会变为null,我也尝试了
'result'+idx.toString()
,但它也不起作用

$.each(cat_result, function(idx, value) { 
text.append('<id="result'+idx+'">'+'<br>');
canvas_and_plot(idx,cat_result[idx][8],cat_result[idx][9],cat_result[idx][10])
});
$('#result').replaceWith(text);
试试这个:

document.getElementById('"result'+idx+'"').appendChild(canv); 

我不知道为什么在多次尝试后,
getElementById
的格式设置不起作用。
我通过使用纯画布HTML在
文本中创建画布。append
工作正常。

您知道如何使用调试器逐步完成JS代码吗?我建议您在浏览器中研究调试器,设置一些断点,然后逐步检查代码以确定什么是null以及为什么是null。getElementById上的id可能不正确,并且返回null,并且null对象不会有.appendChild()方法@谢谢你的回复,我也这么想。如果问题不清楚,对不起。但我仍然不知道getElementById为什么会为null。我刚才也尝试了
'result'+idx.toString()
,但它不起作用。我知道在Chrome中调试的基本方法,但不知道如何进一步调试这个问题。您是否试图以“结果框”元素为目标?@AndyStagg不,我不是,我创建了结果0,结果1。。。使用
并希望将canv_0目标设置为结果_0,将canv_1设置为结果_1等。如果目标设置为结果_0,则。。。getElementById('result_u'+idx)
<p class="result-box" id="result-box">The result is : <br><strong id="result"></strong></p>
function canvas_and_plot(idx,a,b,c) {
var canv = document.createElement("canvas");
canv.width = 200;
canv.height = 200;
canv.setAttribute('id', 'canv_'+idx);
canv.style.position = 'relative';
canv.style.float = 'left';
document.getElementById('result'+idx).appendChild(canv); // Cannot read property 'appendChild' of null
//other code that does the plotting
}
document.getElementById('"result'+idx+'"').appendChild(canv);