[英]Dynamically change the HTML <img> src attribute using Java Script or JQuery
我需要使用Jquery或Java脚本从隐藏文本字段中的值动态更改html img src值。
<p><img id="yui_img" height="333" width="345"></p>
function onImgChange()
{
document.getElementById('yui_img').src=document.getElementById('addProductLinksCreateForm:addProductLinkUrlValue').value;
}
我在DOM中获得了价值,但并没有得到体现。 保留旧图像。
这应该可以,但是您需要确保是这种情况:
文本框标签看起来像<input type="text" id="addProductLinksCreateForm:addProductLinkUrlValue">
吗?
您是否在调用onImgChange()
? 您可以尝试document.getElementById('addProductLinksCreateForm:addProductLinkUrlValue').onchange = onImgChange;
以便在用户切换到其他文本框时发生这种情况。
使用jQuery,它看起来像:
function onImgChange() {
$('#yui_img').attr('src', function(i, src) {
return $('#addProductLinksCreateForm').attr('src');
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.