jquery - 将背景图像 Url 更改为输入字段的值

标签 jquery css click attr

我有一个输入元素、一个 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/

相关文章:

javascript - 使用视差代码更改导航栏干扰的颜色

javascript - 如果 slider 从头开始,Coda-Slider 不应滑回所有元素

javascript - 一个应用程序的自定义 CSS 反射(reflect)在启动板内的每个应用程序中

jquery - 将旧的 html 表转换为响应式

jquery - 编辑自定义 <ul> 数组的文本

javascript - 以编程方式单击 <a>-tag 在 Firefox 中不起作用

jquery - 需要解决 jquery-ui/webkit 中的 .position() 错误

jQuery UI datepicker - 我可以格式化已本地化的日期吗?

javascript - div 中的对象使用 div ID 动态连接

javascript - 有什么方法可以获取文本内容的Element?