【问题标题】:Change placeholder text更改占位符文本
【发布时间】:2012-11-22 05:40:24
【问题描述】:

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

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

【问题讨论】:

  • 你试过$('input').attr('placeholder','Some New Text');吗?
  • 你用 javascript 或 jquery 尝试过什么吗?请发布您尝试过的内容以及遇到的问题。
  • 你可以使用malarkey

标签: javascript jquery html


【解决方案1】:

如果您想使用 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,您可能不需要使用整个库来做这样简单的事情。
  • 通常该字段已经包含一个值,因此占位符将不可见。您必须清除该值。 var email = document.getElementsByName("Email")[0];email.value='';email.placeholder='new text for email';
【解决方案2】:

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

【讨论】:

    【解决方案3】:
    var input = document.getElementById ("IdofInput");
    input.placeholder = "No need to fill this field";
    

    你可以在这里找到更多关于placeholder的信息:http://help.dottoro.com/ljgugboo.php

    【讨论】:

      【解决方案4】:

      使用 jquery,您可以通过以下代码做到这一点:

      <input type="text" id="tbxEmail" name="Email" placeholder="Some Text"/>
      
      $('#tbxEmail').attr('placeholder','Some New Text');
      

      【讨论】:

        【解决方案5】:

        我也遇到了同样的问题。

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

        这是我的解决方案。

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

        【讨论】:

        • 没错。如果不先将该字段设置为空白,则不会显示占位符。花了很长时间,直到我碰到这个帖子。
        【解决方案6】:

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

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

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

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

        【讨论】:

          【解决方案7】:

          对于 JavaScript 使用:

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

          对于 jQuery 使用:

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

          【讨论】:

            【解决方案8】:

            在您的 javascript 文件中

            document.getElementsByName('Email')[0].placeholder = '电子邮件的新文本';

            【讨论】:

            • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
            猜你喜欢
            • 2020-11-28
            • 2017-03-31
            • 1970-01-01
            • 2012-03-03
            • 2017-05-18
            • 2013-02-04
            • 1970-01-01
            • 2016-07-21
            相关资源
            最近更新 更多