[英]asynchronous callback function in a loop
This is kind of different situation then any other question. 这是与其他问题不同的情况。
I have several div elements with class and data-label attributes. 我有几个具有class和data-label属性的div元素。
<div class='toLoad' data-label='a'></div>
<div class='toLoad' data-label='b'></div>
<div class='toLoad' data-label='c'></div>
<div class='toLoad' data-label='d'></div>
Here is the javascript: 这是JavaScript:
function theData(e) {
// What do I do here to put the data to their respective div
}
function createScript() {
var elem = document.querySelectorAll('div.toLoad');
for (var i = 0; i < elem.length; i++) {
var theLabel = elem[i].getAttribute('data-label'),
theLink = "/index/"+ theLabel +"&callback=theData",
currentItem = elem[i],
e = document.createElement('script');
e.src = theLink;
e.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(e);
}
}
After putting the above Javascript, a script element is appended. 放置上述Javascript之后,将添加一个script元素。 After loading, the script returns a callback function named as theData with all content as argument. 加载后,脚本将返回一个名为theData的回调函数,其中所有内容均作为参数。 I want append the returned content to their respective div elements. 我想将返回的内容附加到它们各自的div元素上。
I don't know how to exactly put them in. 我不知道如何准确地放入它们。
Assuming that callback method is available and it has two arguments data-label value (to identity which div should be populated) and the data itself 假设回调方法可用,并且它具有两个参数数据标签值(以标识应填充的div)和数据本身
function callback( dataLabel, htmlData )
{
$( "div[data-label='" + dataLabel + "']".html( htmlData ) );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.