如何更改输入框的占位文本?
例如,我有3个文本类型的输入框。
<input type="text" name="Email" placeholder="Some Text">
<input type="text" name="First Name" placeholder="Some Text">
<input type="text" name="Last Name"placeholder="Some Text">
如何使用JavaScript或jQuery更改某些文本文本?
如何更改输入框的占位文本?
例如,我有3个文本类型的输入框。
<input type="text" name="Email" placeholder="Some Text">
<input type="text" name="First Name" placeholder="Some Text">
<input type="text" name="Last Name"placeholder="Some Text">
如何使用JavaScript或jQuery更改某些文本文本?
如果你想使用Javascript,那么可以使用getElementsByName()
方法来选择input
字段并更改每个字段的placeholder
... 请看下面的代码...
document.getElementsByName('Email')[0].placeholder='new text for email';
document.getElementsByName('First Name')[0].placeholder='new text for fname';
document.getElementsByName('Last Name')[0].placeholder='new text for lname';
否则使用jQuery:$('input:text').attr('placeholder','Some New Text');
var email = document.getElementsByName("Email")[0];
email.value='';
email.placeholder='电子邮件的新文本';
- askrynnikov这个解决方案使用jQuery。如果您想要为所有文本输入使用相同的占位符文本,您可以使用
$('input:text').attr('placeholder','Some New Text');
如果你想要不同的占位符,你可以使用元素的id来更改占位符。
$('#element1_id').attr('placeholder','Some New Text 1');
$('#element2_id').attr('placeholder','Some New Text 2');
var input = document.getElementById ("IdofInput");
input.placeholder = "No need to fill this field";
您可以在这里了解更多关于 placeholder
的信息:http://help.dottoro.com/ljgugboo.php
<input type="text" id="tbxEmail" name="Email" placeholder="Some Text"/>
$('#tbxEmail').attr('placeholder','Some New Text');
我也遇到了同样的问题。
在JS中,首先你需要清空文本输入框,否则占位符文本不会显示。
这是我的解决方案。
document.getElementsByName("email")[0].value="";
document.getElementsByName("email")[0].placeholder="your message";
$('#some_input_id').attr('placeholder','New Text Here');
如果需要,也可以清除占位符:
$('#some_input_id').attr('placeholder','');
对于 JavaScript,请使用:
document.getElementsByClassName('select-holder')[0].placeholder = "This is my new text";
使用jQuery:
$('.select-holder')[0].placeholder = "This is my new text";
$('input').attr('placeholder','Some New Text');
吗? - Jeemusu