Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击按钮时替换图像_Javascript_Jquery_Html_Ajax - Fatal编程技术网

Javascript 单击按钮时替换图像

Javascript 单击按钮时替换图像,javascript,jquery,html,ajax,Javascript,Jquery,Html,Ajax,我试图在点击按钮时替换一个图像,但我似乎不明白。我从服务器上得到一个图像,它看起来像我的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"

我试图在点击按钮时替换一个图像,但我似乎不明白。我从服务器上得到一个图像,它看起来像我的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);
致:

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

//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

您应该更改:

$(".search-result:first").clone().appendTo(".search").find("src").replace(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

为什么你要克隆它两次?好问题,我想我可以克隆一次为什么你要克隆它两次?好问题,我想我可以克隆一次是的,我可以再问一件事吗,因为我从我的服务器上得到了照片,实际上如何显示它,因为我从照片所在的服务器返回路径,如
photo/testImg.png
。我不能把它添加到srcs?你想显示完整路径吗?类似于
http://example.com/img/photo/testImg.png
而不是
photo/testImg.png
?是的,这可能会奏效,因为我可以使用url在浏览器上查看它。但是仅仅添加
attr(“src”)似乎不起作用http://example.com/img/“+照片)
是的,你可以,我用解决方案编辑了答案,编辑到你自己的结构Ye我用我的服务器尝试了这个(已经启动并运行,可以通过url查看图像)我试着将它粘贴到html中,结果成功了,但是appendo和appendo没有像
clone.appendTo(.search”).attr(“src”)中那样工作(硬编码),"http://www.mindshiftinteractive.com/wp-content/uploads/2016/10/test-big.png")
因此,我认为替换代码行不起作用。是的,这起作用了,我可以再问一件事吗,因为我从我的服务器获取照片,我如何实际显示它,因为我从照片所在的服务器获取路径,如
photo/testImg.png
。Surley我不能将其添加到SRC中。你想显示完整路径吗?那么方法类似
http://example.com/img/photo/testImg.png
而不是
photo/testImg.png
?嗯,这可能行得通,因为我可以使用url在浏览器上查看它。但仅仅添加
attr(“src”)似乎行不通http://example.com/img/“+照片)
是的,你可以,我用解决方案编辑了答案,编辑到你自己的结构。我在我的服务器上尝试了这个方法(已经启动并运行,可以通过url查看图像)。我尝试将它粘贴到html中,结果成功了,但是在
clone.appendTo(.search”).attr(“src”)中,appendo和appendo没有起作用(硬编码)http://www.mindshiftinteractive.com/wp-content/uploads/2016/10/test-big.png”;
所以我认为替换代码行不能正常工作