我有一个输入元素、一个 span 和一个带有背景图像属性的 div。
<span>Go</span><input id="ImageUrl"/>
<div id="Image"></div>
我正在尝试创建一个脚本,如果用户在输入字段中粘贴/键入链接,然后单击 Go 按钮,div 的背景图像属性将更改为该 url。
最佳答案
在
中:<script type="text/javascript">
function show() {
$('#Image').css('background-image', 'url("' + $('#ImageUrl').val() + '")');
}
</script>
在 <正文> 中:
<span>Go</span><input id="ImageUrl" /> <!-- the textbox can get the user input Url of Image-->
<div id="Image" style="width:200px; height:200px;"></div> <!-- the div which show Image -->
<input type="button" value="Go" onclick="show()" /> <!-- the GO button -->
如果你有问题请告诉我:)
关于jquery - 将背景图像 Url 更改为输入字段的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5987217/