Html 如何将背景图像放入文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21958760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:00:53  来源:igfitidea点击:

How to put background image inside textbox

csshtml

提问by user3340637

does anyone know how to put background image inside a textbox? i want to do is when i click the textbox it will change the background with an image.Does anyone know how to do that?

有谁知道如何将背景图像放在文本框中?我想做的是当我单击文本框时,它会用图像更改背景。有人知道怎么做吗?

my current code wont work:

我当前的代码不起作用:

<input onfocus="this.style.background='images/activebutton.png'" />

回答by Tun Zarni Kyaw

CSS is the preferredmethod

CSS 是首选方法

CSS

CSS

<style>
    input[type="text"]:focus{
        background-image: url('images/activebutton.png');
    }
</style>

HTML

HTML

<input type="text" />


If you still want to use JavaScript you need to do like this

如果你仍然想使用 JavaScript 你需要这样做

<input type="text" 
onfocus="this.style.backgroundImage='url(images/activebutton.png)';" 
onblur="this.style.backgroundImage=''"
/>

回答by Kawinesh SK

<input class="changeonfocus"/>

CSS

CSS

.changeonfocus:focus{
   background-image: url('image.png');
}

DEMO

演示

回答by gbade_

<input type="text" class="litebox_input">



.litebox_input:focus { 
  background-image: url(images/edit.png);
  background-repeat:repeat-y;
  background-position:right;
}

回答by Simone

To put a background image inside an inputelement you need to set background-imagein CSS:

要将背景图像放入input元素中,您需要background-image在 CSS 中设置:

input {
    background-image: url('image.png');
}

You can do it programmatically via JavaScript by adding/removing a class, or directly using this.style.backgroundImage. So here's an example:

您可以通过 JavaScript 通过添加/删除类或直接使用this.style.backgroundImage. 所以这是一个例子:

<input id="i" type="text" />


var i = document.getElementById('i');

i.addEventListener('click', function() {
  i.style.backgroundImage = "url('image.png')";
});

Demo

演示

回答by ltalhouarne

Use some css styling:

使用一些 css 样式:

inputBox{
  background:url('images/activebutton.png');

}