我可以在输入字段上使用 :before 或 :after 伪元素吗?

我可以在输入字段上使用 :before 或 :after 伪元素吗?

问题描述:

我正在尝试在 input 字段上使用 :after CSS 伪元素,但它不起作用.如果我将它与 span 一起使用,它可以正常工作.

I am trying to use the :after CSS pseudo-element on an input field, but it does not work. If I use it with a span, it works OK.

<style type="text/css">
.mystyle:after {content:url(smiley.gif);}
.mystyle {color:red;}
</style>

这有效(将笑脸放在buu!"之后和更多"之前)

This works (puts the smiley after "buu!" and before "some more")

<span class="mystyle">buuu!</span>a some more

这不起作用 - 它只将 someValue 着色为红色,但没有笑脸.

This does not work - it only colors someValue in red, but there is no smiley.

<input class="mystyle" type="text" value="someValue">

我做错了什么?我应该使用另一个伪选择器吗?

What am I doing wrong? should I use another pseudo-selector?

注意:我不能在我的 input 周围添加 span,因为它是由第三方控件生成的.

Note: I cannot add a span around my input, because it is being generated by a third-party control.

:after:before 在 Internet Explorer 7 及更低版本中的任何元素上均不受支持.

:after and :before are not supported in Internet Explorer 7 and under, on any elements.

它也不适用于替换元素,例如表单元素(输入)和图像元素.

It's also not meant to be used on replaced elements such as form elements (inputs) and image elements.

换句话说,使用纯 CSS 不可能.

In other words it's impossible with pure CSS.

但是如果使用 jquery 你可以使用

However if using jquery you can use

$(".mystyle").after("add your smiley here");

.after 上的 API 文档

使用 javascript 附加您的内容.这将适用于所有浏览器.

To append your content with javascript. This will work across all browsers.