更改占位符文本

IT技术 javascript jquery html
2021-03-08 15:50:44

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

例如,我有 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更改Some Text文本?

6个回答

如果您想使用 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');
+1 在不使用外部库的情况下实现本地化......人们需要注意 DOM API,您可能不需要使用整个库来做一些简单的事情。
2021-04-28 15:50:44
通常该字段已经包含一个值,因此占位符将不可见。您必须清除该值。 var email = document.getElementsByName("Email")[0]; email.value=''; email.placeholder='new text for email';
2021-05-20 15:50:44

此解决方案使用 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

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

<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";
那就对了。如果不先将该字段设置为空白,则不会显示占位符。在这上面花了很长时间,直到我撞到了这篇文章。
2021-05-06 15:50:44