gpt4 book ai didi

javascript - 如何在 html 中单击按钮时更改图像(img URL 每次都来自服务器)

转载 作者:行者123 更新时间:2023-11-28 08:39:28 25 4
gpt4 key购买 nike

我是客户端脚本和 stackoverflow 方面的新手。我想通过单击按钮或 anchor 来更改 div 的图像(图像 URL 每次都来自服务器)。这是我更改图像的代码。

$scope.captchaChange = function () {
$http({
method: "POST",
url: 'http://localhost:8080/Project/captcha/captcha',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).success(function (response) {
if (response.imgUrl.length > 0) {
document.getElementById("captchaImg").src = response.imgUrl;
document.getElementById("captchatext").value = response.imgToken;
} else {
alert('no captcha Image Avalable');
}
}).error(function (response) {
//alert("response" + response)
$scope.codeStatus = response || "Request failed";
return false;
});
}

最佳答案

假设您尝试从服务器加载新图像并替换 div 中的当前图像,您可以这样做:

 $('#img-change-btn').click(function() {
var path = response.imgUrl // Or some path;
$('#img-display-div img').attr('src', path)
.attr('alt', $('img', this).attr('title'));
});

这是工作代码:http://jsfiddle.net/yV6e6/6/

注意:我使用了一些 JQuery(.click、.empty、.append 等),如果您不使用 JQuery 库,则可以将其删除并仅使用 Javascript。

关于javascript - 如何在 html 中单击按钮时更改图像(img URL 每次都来自服务器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20674629/

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