如果页面上只有一个 <input>
,我可以通过 jQuery 放置一个占位符:
<input type="text"/>
$(document).ready(function() {
placeholders();
function placeholders() {
$('input[type=text]').each(function() {
$(this).attr('placeholder', 'hello' );
});
}
});
然而,我在单个页面上有多个字段,如下所示:
<input type="text" class="first">
<input type="text" class="second">
我希望只在第一个 <input type="text">
中添加占位符,该输入框具有 class="first"
。
如何仅为第一个匹配的文本添加占位符?