gpt4 book ai didi

javascript - 单击按钮时替换图像

转载 作者:行者123 更新时间:2023-12-01 03:56:58 24 4
gpt4 key购买 nike

我试图在单击按钮时替换图像,但我似乎不明白。我从我的服务器获取了一张图像,它看起来像我的 ajax 值中的 photo/1583testImg.png 。我现在不想用通过 ajax 从服务器获取的照片替换当前照片。

 for(file in search){
var title = search[file].title;
var photo = search[file].path;
$(".search-result:first").clone().appendTo(".search").find("h3").text(title);
$(".search-result:first").clone().appendTo(".search").find("src").replace(photo); // this is the codeline that doesn't work
console.log(title+"+"+photo);
}

最佳答案

你应该改变:

$(".search-result:first").clone().appendTo(".search").find("src").replace(photo);

至:

$(".search-result:first").clone().appendTo(".search").attr("src",photo);

如果需要显示完整路径,可以使用一些选项:

//window.location.host //you'll get sub.domain.com:8080 or sub.domain.com:80
//window.location.hostname ////you'll get sub.domain.com
//window.location.protocol : you'll get http:
//window.location.port //you'll get 8080 or 80
//window.location.pathname //you'll get /virtualPath

var mainUrl = window.location.protocol+"//"+window.location.hostname;

$(".search-result:first").clone().appendTo(".search").attr("src",mainUrl+"/img/"+photo);

这会给你http://example.com/img/photo/1583testImg.png

关于javascript - 单击按钮时替换图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42577658/

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