如何在 HTML 中的按钮单击上更改图像(IMG URL 每次来自服务器)
how to change image(img URL is everytime coming from the server) on button click in html
我是客户端脚本和堆栈溢出的新手。我想在单击按钮或锚点时更改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。
相关文章:
- 如何在 HTML 中的按钮单击上更改图像(IMG URL 每次来自服务器)
- img src 属性在为空时返回页面的 URL
- EmberJS config prod img src url
- 如何在wordpress插件中获取jquery中的img url和文件url
- JQuery:单击具有特定URL的嵌套Img
- 带有大URL的img请求
- 如何读取存储在图像的数据src中的URL,并在另一个img控制器的src中设置该URL onclick
- 如何更改img url?(带/class)
- Javascript RegExp 解析 URL 使超链接忽略 img src
- 旋转 img 标记中的图像 URL 以预加载所有图像 URL
- AngularJS - img ng-src 到 base64 数据(不是 url)不起作用
- 如何在 img 标记源中使用 blob URL
- 角度,将字符串放在 ng-repeat 中的 url 中(用于 img 链接)
- 比较 IMG SRC + A 包装 URL 字符串,看看它们是否“相似”
- angular ng显示/隐藏取决于img src url
- javascript获取img元素的完整src-url
- 如果IMG SRC中存在URL元素
- 用url和类处理img标签问题
- 使用画布绘制包含SVG源的img以获取数据URL
- 将文本注入“url”;img标签的一部分