Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jQuery/Javascript放置占位符_Javascript_Jquery - Fatal编程技术网

使用jQuery/Javascript放置占位符

使用jQuery/Javascript放置占位符,javascript,jquery,Javascript,Jquery,如果页面上只有一个,我可以通过jQuery放置占位符: $(文档).ready(函数(){ 占位符(); 函数占位符(){ $('input[type=text]')。每个(函数(){ $(this.attr('placeholder','hello'); }); } }); 但是,我在一个页面中有多个字段,如下所示: 我只希望在具有class=“first”的第一个上添加占位符 如何向第一个匹配文本添加占位符仅输入? $('input[type=text].first').attr('

如果页面上只有一个
,我可以通过jQuery放置占位符:


$(文档).ready(函数(){
占位符();
函数占位符(){
$('input[type=text]')。每个(函数(){
$(this.attr('placeholder','hello');
});
}
});
但是,我在一个页面中有多个
字段,如下所示:


我只希望在具有
class=“first”
的第一个
上添加占位符

如何向第一个匹配文本添加占位符
仅输入

$('input[type=text].first').attr('placeholder', 'hello' );

使用选择器可以尝试以下解决方案:
$('input[type=text].first').attr('placeholder','hello')

在这里,
input
将选择所有的
input标签,
[type=text]
将选择类型为text的输入,然后选择
。首先
将选择一个子集,其类为
首先


如果要使用
first
类选择第一个输入,请执行
$('input[type=text].first')。first()

如果要使用特定于元素的脚本,请使用ID而不是类以获得更好的性能

<input type="text" class="first" id="first">

$("#first").attr('placeholder', 'hello' );

$(“#first”).attr('placeholder','hello');
我只想在第一个 具有class=“first”

如何仅向第一个匹配的文本输入添加占位符

尝试将
document.querySelector()
与选择器
“input[type=text][class=first]”
setAttribute()

函数占位符(){
document.querySelector(“输入[type=text][class=first]”)
.setAttribute(“占位符”、“您好”);
}
$(文档).ready(占位符)

<input type="text" class="first" id="first">

$("#first").attr('placeholder', 'hello' );