gpt4 book ai didi

javascript - 附加我的 img src 的代码

转载 作者:行者123 更新时间:2023-11-30 18:02:50 25 4
gpt4 key购买 nike

我想知道是否有人可以帮助我尝试了解错误的原因和可能的解决方案。我正在使用 JavaSript 加载图像,但是当我测试我的页面时,src 属性在 .jpg 的末尾得到一个/。

我的控制台如下所示:

  • 循环:avatars/bugsbunnyundefined
  • 循环:avatars/chimchim.jpg/
  • loop:avatars/christmastree.jpg/
  • 循环:avatars/princess.jpg/
  • 循环:avatars/squarepants.jpg/
  • 循环:avatars/yosemite.jpg/
  • 循环:avatars/wilma.jpg/
  • 循环:avatars/coatandtie.jpg/
  • 循环:avatars/lilymunster.jpg/
  • 循环:avatars/georgejetson.jpg/
  • 循环:avatars/tweety.jpg/
  • 循环:avatars/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');

});

}

});

最佳答案

我觉得问题出在

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

String.replace 方法仅返回更改后的字符串,但更改原始字符串。

String.replace

Description

This method does not change the String object it is called on. It simply returns a new string.

关于javascript - 附加我的 img src 的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16497944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com