2013-05-11 68 views
1

我想知道如果有人可以幫助我試圖知道爲什麼和可能的解決方案,我的錯誤。我使用JavaSript來加載圖像,但是當我測試我的頁面時,src屬性在.jpg的末尾得到一個/。代碼追加我的IMG src

我的控制檯看起來如下:

  • 循環:化身/ bugsbunnyundefined
  • 循環:化身/ chimchim.jpg/
  • 循環:化身/ christmastree.jpg/
  • 循環:化身/ princess.jpg/
  • 循環:化身/ squarepants.jpg/
  • 循環:化身/ yosemite.jpg/
  • 循環:化身/ wilma.jpg/
  • 循環:化身/ coatandtie.jpg/
  • 循環:化身/ lilymunster.jpg/
  • 循環:化身/ georgejetson.jpg/
  • 循環:化身/翠兒.JPG/
  • 循環:化身/ cleveland.jpg/
//JavaScript OBJECT 
    var reviews = [ 
    { Id: "ajjhwejkssl", 
    Title: "The little camera that could!", 
    Rating: 5, Body: "text here", 
    CreateDate: new Date(2012,5,23,14,12,10,0), 
     Owner: { 
     Id: "kwergiueerwq", 
     Name: "Bugs Bunny", 
     Url: "./users.html?id=kwergiueerwq", 
     AvatarImage: "avatars/bugsbunny", 
     IsFeaturedReviewer: false, 
     CreateDate: new Date(2012,2,12,9,44,0,0) 
     } 
    }] 



    var data = reviews; 
    var newDiv = null; 
    var my_div = null; 
    var my_img = null; 
    var total = document.getElementById('total'); 
    var review = $('#reviews'); 
    $(document).ready(function(){ 


    for (var i = 0; i < data.length; i++){ 
     console.log("loop: " + data[i].Owner.AvatarImage + rand); 
     var rand = ".jpg/"; 
     rand.replace(rand , ".jpg"); 

     //CREATE NEW REVIEW DIV 
     var reviewPost = "<div class='review'><div class='clear'></div><div class='content'><div class='datePosted'>" + data[i].CreateDate + "</div><div class='avatar'><div class='header'><div class='rating'><img src='images/star-sprite.png'/><img src='images/star-sprite.png'/><img src='images/star-sprite.png'/><img src='images/star-sprite.png'/><img src='images/star-sprite.png'/></div></div><div class='clear'></div><div class='title'>" + data[i].Title + "</div><div class='memberImg'><img class='userImg' src=" + data[i].Owner.AvatarImage + '.jpg'+"/></div><div id='member'><div class='reviewedBy'>Reviewed by <a href='"+data[i].Owner.Url+"' class='member'>" + data[i].Owner.Name + "</a></div><div class='membership'>Member Since " + data[i].Owner.CreateDate + "</div></div></div></div><div class='clear'></div><div class='message'>" + data[i].Body + "</div></div><div class='clear'></div>"; 

     //adds reviewPost inside of reviews 
     review.append(reviewPost); 

    $.each(".userImag" , function(){ 
     //console.log("data: " + data[i].Owner.AvatarImage); 
     $(this).attr('src', data[i].Owner.AvatarImage + 'jpg'); 

    }); 

    } 

    }); 

回答

0
var rand = ".jpg/"; 
rand.replace(rand , ".jpg"); 

這沒有任何意義。您asigning變量的值,並通過自身._0替換變量

你可能想是這樣的:

var StringContainingFilePath; 
var search = ".jpg/"; 
var replace = ".jpg"; 
StringContaingingFilePath = StringContaingingFilePath.replace(search,replace); 
1

我認爲這個問題是在

var rand = ".jpg/"; 
rand.replace(rand , ".jpg"); 

String.replace方法只返回一個更改的字符串,但要做不要更改原來的一個。

String.replace

說明

此方法不改變它被稱爲在字符串對象。它只是返回一個新的字符串。

1

這個代碼看看:

console.log("loop: " + data[i].Owner.AvatarImage + rand); 
var rand = ".jpg/"; 
rand.replace(rand , ".jpg"); 

第一行要添加「+蘭特」尚未確定其蘭特。 您正在設置rand變量的第二行 而第三行幾乎被忽略,因爲沒有變量被分配給它。我不認爲.jpg /實際上在你的圖片源中。

Josh