【发布时间】:2016-12-30 21:08:29
【问题描述】:
目标:使用动态获取并存储为 JSON 的图像创建我的 墙。
问题:墙运行良好,但即使我注入的element 没有有效的img src,也会显示一些节点。
问题:如何防止这种情况发生?当且仅当图像 src url 无效时,我需要找到一种方法来排除将 elements 注入我所在的当前 node 的方法。在我的控制台中,当我执行此操作时收到 404 http 错误:
var a = new Element("img[src=" + profilePic + "]");
但我不知道如何捕获 404 并阻止它被注入。这是我的墙代码:
var wall = new Wall("myWall", {
"draggable":true,
"autoposition":true,
"inertia":true,
"width":275,
"height":275,
"slideshow":true,
"transition":Fx.Transitions.Expo.easeInOut,
"showDuration": 3000,
callOnUpdate: function(items){
items.each(function(e, i){
while (counter < maxLength) {
var tw = tweets[counter];
if (tw && tw.author && tw.author.picture_url) {
var profilePic = tw.author.picture_url;
if (tw.author.data_source === "twitter-user") {
profilePic = (tw.author.picture_url).replace("_normal", "");
}
else if (tw.author.data_source === "facebook-user") {
profilePic = tw.author.picture_url + '?type=large';
}
// var http = new XMLHttpRequest();
// http.open('HEAD', profilePic, false);
// // http.setRequestHeader("Access-Control-Allow-Origin", "*");
// http.send();
// if (http.status === 404) {
// console.log("BAD IMAGE!!!");
// }
// ims[i].src = 'https://unsplash.it/150';
var a = new Element("img[src=" + profilePic + "]");
if (a === null) {
console.log("a is null");
}
a.height = 250;
a.width = 250;
e.node.setStyle("background", "no-repeat center center");
a.inject(e.node).fade("hide").fade("in");
a.addEvent("click", function (e) {
if (!wall.getMovement()) {
showModal(tw);
}
});
break;
} else {
counter++;
}/*else {
a = new Element("img[src=https://unsplash.it/150?image="+counter+"]");
//a.node.setStyle("background",colors[ Math.floor(Math.random()*colors.length) ]);
a.inject(e.node).fade("hide").fade("in");
}*/
}
counter++;
if( counter > maxLength ) counter = 0;
}.bind(this));
}.bind(this)
});
【问题讨论】:
标签: javascript html angularjs user-interface mootools