开发者

CSS/jQuery: background color for JUST the text in an input

开发者 https://www.devze.com 2023-01-30 19:33 出处:网络
I have some readonly inputs. I want JUST the text in these inputs t开发者_JS百科o have a background color. Is there a way to accomplish this with CSS/jQuery? I\'ve tried:

I have some readonly inputs. I want JUST the text in these inputs t开发者_JS百科o have a background color. Is there a way to accomplish this with CSS/jQuery? I've tried:

$('.show input').contents().css('background-color', '#F0444D');

and

$('.show input').children().css('background-color', '#F0444D');

But neither did anything.


Using lynxforest's answer you can set the background of the whole field to a certain colour.

I understand you want just the text to have the background colour, not the whole field. But that's not possible in an INPUT field. In another kind of element such as a DIV you could insert a SPAN tag:

<DIV class="box"><SPAN>blabla</SPAN><DIV>

but INPUT fields can have no other elements as content so this trick doesn't work.


Have you tried?

$('.show input').css('background-color', '#F0444D');


this will only set a background color with texted fields

$('input[type=text]').each(function() {
    if ($(this).val().length == 0) $(this).css('background', '#CCC')
})
0

精彩评论

暂无评论...
验证码 换一张
取 消