Javascript 更改占位符文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13506481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 12:14:04  来源:igfitidea点击:

Change placeholder text

javascriptjqueryhtml

提问by jeponkz

How can I change the placeholder text of an input element?

如何更改输入元素的占位符文本?

For example I have 3 inputs of type text.

例如,我有 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">

How can I change the Some Texttext using JavaScript or jQuery?

如何使用 JavaScript 或 jQuery更改Some Text文本?

回答by NazKazi

If you wanna use Javascript then you can use getElementsByName()method to select the inputfields and to change the placeholderfor each one... see the below code...

如果您想使用 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';

Otherwise use jQuery:

否则使用jQuery:

$('input:text').attr('placeholder','Some New Text');

回答by arulmr

This solution uses jQuery. If you want to use same placeholder text for all text inputs, you can use

此解决方案使用 jQuery。如果要对所有文本输入使用相同的占位符文本,可以使用

$('input:text').attr('placeholder','Some New Text');

And if you want different placeholders, you can use the element's id to change placeholder

如果你想要不同的占位符,你可以使用元素的 id 来改变占位符

$('#element1_id').attr('placeholder','Some New Text 1');
$('#element2_id').attr('placeholder','Some New Text 2');

回答by padma

var input = document.getElementById ("IdofInput");
input.placeholder = "No need to fill this field";

You can find out more about placeholderhere: http://help.dottoro.com/ljgugboo.php

您可以placeholder在此处了解更多信息:http: //help.dottoro.com/ljgugboo.php

回答by Rusty

Using jquery you can do this by following code:

使用 jquery,您可以通过以下代码执行此操作:

<input type="text" id="tbxEmail" name="Email" placeholder="Some Text"/>

$('#tbxEmail').attr('placeholder','Some New Text');

回答by Lulu Pte

I have been facing the same problem.

我一直面临同样的问题。

In JS, first you have to clear the textbox of the text input. Otherwise the placeholder text won't show.

在JS中,首先你必须清除文本输入的文本框。否则不会显示占位符文本。

Here's my solution.

这是我的解决方案。

document.getElementsByName("email")[0].value="";
document.getElementsByName("email")[0].placeholder="your message";

回答by Rajitha Alluri

Try accessing the placeholder attribute of the input and change its value like the following:

尝试访问输入的占位符属性并更改其值,如下所示:

$('#some_input_id').attr('placeholder','New Text Here');

Can also clear the placeholder if required like:

如果需要,还可以清除占位符,例如:

$('#some_input_id').attr('placeholder','');

回答by Murtaza Manasawala

For JavaScript use:

对于 JavaScript 使用:

document.getElementsByClassName('select-holder')[0].placeholder = "This is my new text";

For jQuery use:

对于 jQuery 使用:

$('.select-holder')[0].placeholder = "This is my new text";