如何在ie7或更高版本中设置密码字段的占位符
How to set the placeholder for password field in ie7 or higher
我需要在密码字段中显示一些默认文本,如"输入您的密码"。由于我需要支持IE7,占位符属性在这里不是解决方案。我用过对焦和模糊,但在IE浏览器中不起作用。
<input type="text" value="Enter the password" id="fieldPassword" onfocus="showPass()" onblur="hidePass()"/>
function showPass()
{
var pass = document.getElementById('fieldPassword').value;
if((pass=="Enter the password")||pass=="")
{
document.getElementById('fieldPassword').type="password";
document.getElementById('fieldPassword').value="";
}
else
{
}
}
function hidePass()
{
var pass = document.getElementById('fieldPassword').value;
if(pass=="")
{
document.getElementById('fieldPassword').type="text";
document.getElementById('fieldPassword').value="Enter the password";
}
}
为了实现这一点,您必须使用
<input type='text' value='Enter your password' />
点击/聚焦时,您将把输入元素的类型更改为
<input type='password' />
在模糊时,如果值没有变化,您将把它改回文本类型。您也可以使用显示/隐藏的两个不同输入来实现这一点,但在这种情况下,您必须触发对新可见元素的关注,并且不能对两者使用相同的id。
请参阅http://jsfiddle.net/9GTcL/2/
HTML:
<div class="placeholder">
<input type="password" id="pswdInput" />
<input type="text" value="Enter password..." id="textInput" />
</div>
JavaScript:
var text=document.getElementById('textInput'),
pswd=document.getElementById('pswdInput');
text.onfocus=pswd.onfocus=function(){
text.style.display='none';
pswd.focus();
}
pswd.onblur=function(){
if(pswd.value==''){
text.style.display='block';
}
}
CSS:
.placeholder{
position:relative;
}
.placeholder>#textInput{
position:absolute;
top:0;
left:0;
}
.placeholder>input{
width:140px;
}
这里有一个js文件,我用它在IE中有占位符。只需下载它并将其链接到页面的标题中,然后在结束正文标记之前添加这段代码:
<script type="text/javascript">$('input[placeholder], textarea[placeholder]').placeholder();</script>
相关文章:
- 如何在ie7或更高版本中设置密码字段的占位符
- 为每个页面设置密码保护
- 使用Cookie通过Javascript设置用户名和密码
- 可以't散列密码并将其设置为mongodb模式中的字段
- 使用 JS 设置网站的用户名和密码
- 使用 Jquery 将占位符设置为文本框类型密码
- 如果用户在发布之前没有设置密码,请重定向用户
- 如何在 Node.js 中将过期设置为 24 小时内生成的密码
- 在网页上设置可下载文件的密码保护
- 如何在javascript中为放置在引导模式中的表单设置密码和用户名
- 在不设置服务的情况下安全地存储数据库密码
- 为流星中的自定义路由设置重置密码令牌
- 在数组中设置密码,用户只有3次机会
- Facebook API+设置密码和用户名+PHP
- 不能登录脚本,即使我有用户和密码设置在我的数据库
- 我如何在javascript中为我的html网站设置密码
- 如何设置“password123”作为我的密码在HTML登录页面
- 设置密码字符数的If条件
- 测试受密码保护的URL在硒中的设置是什么
- 不能让登录脚本只接受设置的密码