2016-08-23 4 views
0

目標なしノードに要素を注入します。ウォール(MooToolsはプラグイン)画像

問題:壁が美しく動作しますが、いくつかのノードが現れている場合でも、その私は、有効なimg srcを持っていない注射よelement

質問:これを防ぐにはどうすればよいですか?私は、nodeの画像にelementを挿入することを排除する方法を見つける必要があります。画像src urlが有効でない場合にのみ表示されます。

var a = new Element("img[src=" + profilePic + "]"); 

しかし、私は404とこれを注入することを妨げることをキャッチすることができるか分からない。私のコンソールでは、私はこれを行うとき、私は HTTPエラーを受け取ります。ここに私のウォール・コードがあります:

FIXED
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) 
         }); 

答えて

0

私はちょうど私のCSSクラスに基づいて、次を追加しました:

$(".prof").error(function(){ 
    $(this).attr('src', '../assets/images/tile/person-placeholder.jpg'); 
}); 
関連する問題