在css中向占位符添加文本

时间:2013-02-03 10:54:54

标签: css placeholder

<input type='text' />

我需要在css中添加占位符文本,如下所示:

input:placeholder{content: 'placeholder text';}

2 个答案:

答案 0 :(得分:1)

您无法为所有浏览器使用CSS设置占位符。目前唯一支持它的浏览器是webkit。

看看这个问题:How to set placeholder value using CSS?

答案 1 :(得分:1)

你不能用css这样做。但是你可以用jQuery完成这个,如下面的演示所示。

$(document).ready(function() {
    placeholders();
    function placeholders() {
        var count = 0;
        $('input[type=text]').each(function() {
            count++;
			$(this).attr('placeholder', 'value ' + count);
        });
    }
    
    $(document).on('click', '.delete', function() {
    	$(this).closest('div').remove();
        placeholders();
    });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
    <input type="text"/><button class="delete">DELETE</button>
</div>
<div>
    <input type="text"/><button class="delete">DELETE</button>
</div>
<div>
    <input type="text"/><button class="delete">DELETE</button>
</div>
<div>
    <input type="text"/><button class="delete">DELETE</button>
</div>