我只是想将html导入一个新节点,并删除带有'.prototype'类的元素:
var statics = new Element('div');
statics.load('statics.html')
statics.getElements('.prototype').destroy();
statics.inject($('main'));
这不能按预期工作。类“.prototype”的元素仍然存在。有什么见解吗?
答案 0 :(得分:2)
如果代码没有改变,那么摘录的问题是Request.HTML
使异步XMLHttpRequest
。
当.load()
被调用时,它会XMLHttpRequest
到statics.html
,但在它可以返回HTML有效负载之前,脚本的执行会继续,.getElements('.prototype')
是运行statics
,目前为空div
,因此找不到任何元素。
要作弊,您可以更改ajax请求以使其异步:
statics.set('load', { async: false });
但是,在保持异步性的同时执行此操作的正确方法是使用onSuccess
回调:
statics.set('load', {
onSuccess: function() {
var toDestroy = statics.getElements('.prototype'),
numDestroy = toDestroy.length,
x;
for(x=0;x<numDestroy;x++) {
toDestroy[x].destroy();
}
}
});