如何使用JavaScript删除/更改输入值 [英] How to remove/change an input value using javascript

查看:68
本文介绍了如何使用JavaScript删除/更改输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在此输入中删除值"Anonymous",并使用javascript/jquery将其替换为占位符文本您的姓名"?我无权访问HTML代码.

How do I remove the value 'Anonymous' in this input that I have and replace it with a placeholder text 'Your name' using javascript/jquery? I don't have access to the HTML code.

这是我到目前为止所拥有的,但是真的不知道从那里去哪里.

This is what I have so far, but don't really know where to go from there.

document.getElementById('txtYourName').placeholder =' Your Name ';  


HTML


HTML

<input id="txtYourName" type="text" value="Anonymous" name="txtYourName"></input>

推荐答案

在此添加第二行...仅当值碰巧为空白时,才会看到占位符.使用下面的代码,您可以设置占位符,以及清除字段中的默认值.

Add this second line here... A placeholder is only seen when the value happens to be blank. With the code below, you can set the placeholder, as well as erase the default value in your field.

document.getElementById('txtYourName').placeholder =' Your Name ';  
document.getElementById('txtYourName').value = "";

演示: http://jsfiddle.net/723vL/

这篇关于如何使用JavaScript删除/更改输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆